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; ?>

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

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.