Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

3 Answers 3

up vote 6 down vote accepted
  • 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>
share|improve this answer
    
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

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;
?>
share|improve this answer
    
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. –  toscho Mar 25 '11 at 22:58

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; ?>
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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