为第一个、第二个和第三个帖子显示不同大小的特色图片(_POST_缩略图)?

时间:2012-05-07 作者:its_me

我将博客上的所有图库都放在“图库”标签下。我使用以下代码(via) 要在主页上第8篇最新帖子之后显示最新的7篇画廊帖子,请执行以下操作:

<!-- Featured post galleries after 8th post -->
<?php
   $homegallery++;
   if ($homegallery == 8){
?>

<!-- Display Gallery Posts -->
<div id="gallery-posts">
<?php
    $count = 0;
    $some_featured_posts = new WP_Query(array(\'category_name\' => \'apps\', \'posts_per_page\' => 5));
    while ($some_featured_posts->have_posts()): 
        $some_featured_posts->the_post();
        $count++;
?>

<?php the_post_thumbnail( \'thumbnail\' ); ?>

<?php
    endwhile;
    wp_reset_postdata();
?>
</div><!-- Display Gallery Posts END -->

<?php
   };
?><!-- Featured post galleries END -->
如您所见,我使用缩略图来表示每个帖子<?php the_post_thumbnail( \'thumbnail\' ); ?>, 可能看起来很奇怪,但我就是这么做的。但是我想展示前两篇文章的缩略图,一个尺寸(大),第三个尺寸(中),其余尺寸(缩略图)。

我该怎么做?

如果有帮助的话,我已经通过一些帮助做到了这一点:

<?php
    $loop_first = 1;
    if ( ! isset ( $loop_first ) ) {

    // set featured image size for first post
    the_post_thumbnail( \'medium\' );
    } else {

    // set featured image size for other posts
    the_post_thumbnail( \'thumbnail\' );
    }
?>
以上代码(我相信)允许我为第一篇帖子显示不同大小的图像,为其余帖子显示不同大小的缩略图。

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

只需使用$count 要递增的变量:

<?php
    $count = 0;
    $some_featured_posts = new WP_Query(array(\'category_name\' => \'apps\', \'posts_per_page\' => 5));
    while ($some_featured_posts->have_posts()): 
        $some_featured_posts->the_post();
        $count++;

        if( $count <= 2 )
          the_post_thumbnail( \'large\' ); 

        elseif( 3 == $count )
          the_post_thumbnail( \'medium\' ); 

        else
          the_post_thumbnail( \'thumbnail\' ); 


    endwhile;
    wp_reset_postdata();
?>

SO网友:Gul ahmad

是的,这是个好主意,但问题是它会重复每个条件下的所有图像,我的意思是它会显示所有图像,而我们这里有一个条件,比如。。。

<?php $count = 0;
    $some_featured_posts = new WP_Query(array(\'category_name\' => \'latest-products\', \'posts_per_page\' => 6));
    while ($some_featured_posts->have_posts()): 
        $some_featured_posts->the_post();
        $count++;

    if( $count == 0 )?>
    <div class="first-row" style="float:left">
    <div class="first-img"><?php the_post_thumbnail( array(200,400) ); ?></div>
   <?php  if  ( $count == 1 )  ?>
    <div class="second-img"><?php the_post_thumbnail( array(100,100) ); ?></div>

    </div>
     <?php if( 2 == $count ) ?>
      <div class="second-row" style="float:left">
    <div class="secondrow-first-img"><?php the_post_thumbnail( array(200,300) ); ?></div>
          <?php if( 3 == $count ) ?>
    <div class="secondrow-second-img"><?php the_post_thumbnail( array(100,75) ); ?></div>
     <?php if( 4 == $count ) ?>
    <div class="secondrow-third-img"><?php the_post_thumbnail( array(400,300) ); ?></div>

    </div>
     <?php if( 5 == $count ) ?>
      <div class="third-row" style="float:left">
    <div class="thirdrow-first-img"><?php the_post_thumbnail( array(200,500) ); ?></div>


    </div>

结束

相关推荐

从unctions.php调用的Add_Actions未返回好值

我正试图通过这些功能为我的网站添加一些安全/访问防护。php。然而,每当我尝试通过函数添加时。php(而不是作为插件,我以前做过)失败(总是返回false)。例如:add_action(\"parse_query\", checkaccess()); // in functions.php 以及function checkaccess() { $allowAccess = false; if(is_admin()||is_front_page()||i