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.

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">
        <ul>
          <?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) );?>
            </span>
            <?php the_title(); ?>
            </a></li>
          <?php endwhile; $count++; endif; ?>
        </ul>
      </div>
share|improve this question
add comment

1 Answer

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
add comment

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.