0

I have this code to show all post of category and thumbnail of them.


<?php $recent = new WP_Query(); ?>
<?php $recent->query('cat=1&showposts=5'); ?>
<?php while($recent->have_posts()) : $recent->the_post(); ?>
<ul>
    <li>
            <a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            </a>
       </li>
</ul>
<?php endwhile; ?>

But now I only want show thumbnail for first post of category. clearly,ex Category have 4 post, I show 4 post but only first post have thumbnail,3 posts remain only have title and permalink

raina77ow
  • 103,633
  • 15
  • 192
  • 229
Van Tong
  • 23
  • 1
  • 2
  • 5

3 Answers3

1

quick fix could be adding a counting variable..

<?php i = 1; ?>
<?php while($recent->have_posts()) : $recent->the_post(); ?>

<ul>
    <li>
<?php if(i==1){ 
  // code to display thumbnail
 } ?>

            <a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            </a>
       </li>
</ul>
<?php i++; ?>
<?php endwhile; ?>
kingkode
  • 2,220
  • 18
  • 28
1

Add the_post_thumbnail to your output and include a $postNumber to track what number post you're on. Then, with an if statement, you can include the_post_thumbnail call. If you want to include it on the first 2, change the if to $postNumber <= 2

<?php $recent = new WP_Query(); 
<?php $recent->query('cat=1&showposts=5'); ?>
<?php $postNumber = 1; ?>
<?php while($recent->have_posts()) : $recent->the_post(); ?>
<ul>
    <li>
            <a href="<?php the_permalink(); ?>">
            <?php 
                if($postNumber<=1){
                    the_post_thumbnail();
                }
                $postNumber++;
             ?> 
            <?php the_title(); ?>
            </a>
       </li>
</ul>
<?php endwhile; ?>
0
<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>

<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
    <ul>
        <li>
                <a href="<?php the_permalink(); ?>">
                <?php the_title(); ?>
                </a>

                <?php 
                if ( $is_first_post  && has_post_thumbnail() ) {
                    the_post_thumbnail(); 
                    $is_first_post = false; 
                }
                ?>


           </li>
    </ul>
<?php endwhile; ?>
Jared
  • 12,406
  • 1
  • 35
  • 39