How to show a featured image as current?

How you want to highlight this image is specific to your layout, but it probably involves setting a class to the <li> surrounding it? You can do this by comparing the ID of the post you have displayed with the ID of the post of which you are showing the thumbnail.

In your first loop you save the ID:

$current_testimonial_id = get_the_ID();

In your second loop you compare it to the current thumbnail:

<li <?php if ( get_the_ID() == $current_testimonial_id ) { echo ' class="highlighted"'; } ?>>

A quick tip if you are working with multiple loops: most template functions accept post ID parameters, so you don’t have to use the setup_postdata() function which sets some global variables. This is handy if you need the “main” post query again after your extra loop. So the following code works just a well for the second loop:

    <ul class="portfolio">
      <?php
 $testimonials = get_posts('category_name=testimonial&numberposts=-1&order=DESC');
 foreach ($testimonials as $testimonial) : 
      ?>
      <li <?php if ( $testimonial->ID == $current_testimonial_id ) { echo ' class="highlighted"'; } ?>> 
        <?php echo get_the_post_thumbnail( $testimonial->ID, 'nav' ); ?>
      </li>
      <?php endforeach; ?>
    </ul>

Leave a Comment