3

What's the best way to use <?php the_post_thumbnail();?> in my loop BUT only show a thumbnail on the FIRST post? Meaning, only the first post in the loop will have it's image shown?

Here is an example of a loop that shows the image for ALL posts:

<!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the posts Image thumbnail for the post -->
<?php the_post_thumbnail();?>
 <!-- Display the date and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
 <!-- Display the Post's Content in a div box. -->
 <div class="entry">
   <?php the_content(); ?>
 </div>

Thank you!

13
  • add a variable before the loop (before the while), for example $first = true;
  • add a check inside the loop for this variable
  • after the use, change the flag

Code:

<!-- Start the Loop. -->
 <?php $first = true; ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the posts Image thumbnail for the post -->
    <?php if ( $first ): ?>
      <?php the_post_thumbnail();?>
      <?php $first = false; ?>
    <?php endif; ?>
 <!-- Display the date and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
 <!-- Display the Post's Content in a div box. -->
 <div class="entry">
   <?php the_content(); ?>
 </div>
  • I went with this solution as I found it most elegant and easy to understand.Thank you for the response. – Pwn Mar 25 '11 at 19:33
4

This code in your template will display the post thumbnail only for the first post:

<?php 
    ! isset ( $loop_first ) and the_post_thumbnail();
    $loop_first = 1;
?>
  • Thank you for the response. Would you not have to set $loop_first=0 or something afterwards? – Pwn Mar 25 '11 at 19:34
  • 1
    @Pwn, The isset() saves you from creating a variable first. – fuxia Mar 25 '11 at 22:58
3

This is what I use in my projects and it works well for me. I modified the code you provided to suit. Simply drop it in and it will display the post thumbnail only for the first post.

<!-- Start the Loop. -->
 <?php $i = 1 ; ?>
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <!-- Display the Title as a link to the Post's permalink. -->
 <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<?php if ($i == 1): ?>
<!-- Display the posts Image thumbnail for the post -->
<?php the_post_thumbnail();?>
<?php endif; ?>
 <!-- Display the date and a link to other posts by this posts author. -->
 <small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
 <!-- Display the Post's Content in a div box. -->
 <div class="entry">
   <?php the_content(); ?>
 </div>
<?php $i++; endwhile; endif; ?>
  • Yes, that looks like it would work well too. I went with the above solution above instead because i found it simpler to use. Thank you for your time, cheers – Pwn Mar 25 '11 at 19:37
0

Just check the current_post value

global $wp_query; // get the global query - works in custom queries too
if(0 == $wp_query->current_post){ /**is the first post**/ }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.