WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following code which should add a last class to the fourth post but doesn't appear to be, what is wrong with this coding? It show's 4 posts fine but the fourth one doesn't have the class last attached to it.

      <div class="headline-strip">
          <?php if (have_posts()) : while (have_posts()) : the_post(); $count = 1;
  if( $post->ID == $do_not_duplicate ) continue; ?>
          <li <?php if ($count == 4) : ?>class="last"<?php endif; ?>><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title(); ?>"><span>
            <?php the_post_thumbnail( array (176,176) );?>
            <?php the_title(); ?>
          <?php endwhile; $count++; endif; ?>
share|improve this question
up vote 2 down vote accepted

$count = 1; is set in the while loop, so every time the loop runs it sets to 1 again. Place it before your loop and it will work.

$count = 1; if (have_posts()) : while (have_posts()) : the_post();

Ow and the $count++ is outside the loop so it will only run after all the posts. Change that to:

<?php $count++; endwhile; endif; ?>
share|improve this answer

Your Answer


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.