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

Seems like a trivial problem but I can't get it to work. I need to add class="last" to every third post.

Here is my code:

     $count = 0;
     $my_query = new WP_Query('cat=-18,-7&showposts=9');
     while ($my_query->have_posts()) : $my_query->the_post();

    <article class="<?php if ($count % 3 == 0) { echo "last "; }" ?>> </article>

share|improve this question
Is the missing '?>' after the if statement just a typo or is that copy/pasted from your code? – sanchothefat Jan 10 '11 at 19:23
Yes, it's a typo. Fixed it. – Eeyore Jan 10 '11 at 19:34
Just checking :) – sanchothefat Jan 10 '11 at 19:42
You should increment the counter at the start of the loop, or alternatively set the initial value to 1, else your counter has a starting value of zero for the first result. (edit: should have scrolled down, my bad). – t31os Jan 10 '11 at 22:15
Do you have the final code for this? – user4792 Apr 20 '11 at 15:12
up vote 1 down vote accepted

I think you just need to start the $count variable from 1 and not zero. You'll get the opposite effect otherwise because 0 modulo 3 is 0. The first item of every 3 will be getting the class name.

share|improve this answer
This doesn't work for me. ONLY the third item in each rows has to have the class="last" – Eeyore Jan 11 '11 at 3:56
Yeah. Starting the count from 1 does what you want in my own tests. Can you explain what effect you're actually seeing with the counter starting on 1 so I can help? – sanchothefat Jan 11 '11 at 10:37
@Eeyore: If you want only the third item, you should not use ($count % 3 == 0) but ($count == 3). If you want items # 3, 6, 9, ... to have this class you use the modulo (%). – Jan Fabry Jan 11 '11 at 11:38

I am not sure what you need mod operator here for, it is usual for keeping track of even/odd values. You simply need every third.

$i = 0;

//while stuff
    if( 3 == $i ) {
        $i = 0;
        echo 'last';
share|improve this answer

start the count from 1

post number count value %value
1                1          1
2                2          2
3                3          0
share|improve this answer

I found this looking for a way to do the same for use with the WordPress starter theme, 'Bones'. It also has a 'first' class. I post that here in case it helps anybody else.

<div class="wrap clearfix">
$count = 1;
$categories = get_categories($args);
foreach ($categories as $cat) { ?>
    <article class="
            //echo $count.' ';
            if ($count % 3 == 0) {echo 'last ';} 
            if ($count == 1 || $count % 3 == 1) {echo 'first ';}
        <h4><?php echo $cat->cat_name ?></h4>
        <div class="desc"><?php echo $cat->category_description ?></div>
<?php $count++;} ?>

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.