2

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; 
    ?>
5
  • 1
    Is the missing '?>' after the if statement just a typo or is that copy/pasted from your code? Jan 10, 2011 at 19:23
  • Yes, it's a typo. Fixed it.
    – Eeyore
    Jan 10, 2011 at 19:34
  • Just checking :) Jan 10, 2011 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, 2011 at 22:15
  • Do you have the final code for this?
    – user4792
    Apr 20, 2011 at 15:12

4 Answers 4

1

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.

3
  • This doesn't work for me. ONLY the third item in each rows has to have the class="last"
    – Eeyore
    Jan 11, 2011 at 3:56
  • 1
    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? Jan 11, 2011 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, 2011 at 11:38
1

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
    $i++;
    if( 3 == $i ) {
        $i = 0;
        echo 'last';
    }
1

start the count from 1

post number count value %value
1                1          1
2                2          2
3                3          0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.