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:
<?php
$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>
<?php
$count++;
endwhile;
?>
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.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.
start the count from 1
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.