【问题标题】:WordPress get post thumbnails in a custom patternWordPress 以自定义模式获取帖子缩略图
【发布时间】:2015-01-25 22:44:09
【问题描述】:

现在我为每篇文章提供两种尺寸的缩略图:

$big = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_600x200' );
$small = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_200x100' );

我想要实现的是使用下一个模式显示帖子:

帖子 1 - [大缩略图]
帖子 2 - [小缩略图]
帖子 3 - [小缩略图]
帖子 4 - [大缩略图]
帖子 5 - [小缩略图]
帖子 6 - [小缩略图]

实际上帖子会显示大-小-小-大-小-小等等。

有什么想法吗?谢谢

这是我的帖子:

<?php foreach ($posts as $post) { 
    $big = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_600x200' );
    $small = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_200x100' );

    if ( $big ) { ?>
        <img src="<?php echo $big['0']; ?>" />
    <?php }else{ ?>
        <img src="http://placehold.it/600x200/7f8c8d/ffffff" alt="Featured image missing"/>
    <?php } ?>
<?php } ?>

【问题讨论】:

  • @Rikesh 你能帮我解决这个问题吗?怎么可能?
  • @Rikesh 为每个添加了我的
  • @Helper 我的解决方案有效吗?
  • @user2997779 是的。谢谢

标签: php html wordpress thumbnails codex


【解决方案1】:

在函数外创建一个计数器。

在函数内部,递增计数器。但在此之前,请检查它是否计数 % 3 == 0。

如果是,请显示大缩略图。

<?php
 $counter = 0;
 foreach ($posts as $post) {  
        if($counter %3 == 0)
        {
           $big = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_600x200' );
        }else{
           $small = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail_200x100' );
        }

    if ( $big ) { ?>
        <img src="<?php echo $big['0']; ?>" />
    <?php }else{ ?>
        <img src="http://placehold.it/600x200/7f8c8d/ffffff" alt="Featured image missing"/>
    <?php } ?>
  counter++; //increase the counter
<?php } ?>

【讨论】:

    【解决方案2】:

    如何为每个帖子增加一个指标,从值 3 开始,你总是做一个模

    if(($i % 3) == 0) { 
      use big 
    } else {
      use small
    }
    $i++;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多