0

Possible Duplicate:
How to mark every 3rd post

I'm trying to give a class to every third list item on my page. I know how to give a class to every item, but not to specific items in a row, or in my case - the third.

This is the code I am using:

<?php
            $pages = get_pages('child_of=10');
            if ($pages) {
                echo '<ul class="projectthumbs">';
                foreach ($pages as $page) {
                    echo '<li><a href="'.get_permalink($page->ID).'">';
                    echo get_the_post_thumbnail($page->ID);
                    echo '<span class="projectthumbtitle">';
                    echo get_the_title($page->ID);
                    echo '</span>';
                    echo '</a></li>';
                }
                echo '</ul>';
            }
            ?>

I know I need to use a counter (and probably something to reset it after each third list item) but I am unsure of how to code this logic in PHP. Can anyone assist?

Thanks

0

1 Answer 1

2

add a simple counter and conditional check:

$pages = get_pages('child_of=10');
$counter =  1;
if ($pages) {
    echo '<ul class="projectthumbs">';
    foreach ($pages as $page) {
        if ($counter == 3){
            $class = ' class="YOUR_CLASS"';
            $counter = 1
        }else{
            $class = '';
            $counter = $counter +1;
        }

        echo '<li'.$class.'><a href="'.get_permalink($page->ID).'">';
        echo get_the_post_thumbnail($page->ID);
        echo '<span class="projectthumbtitle">';
        echo get_the_title($page->ID);
        echo '</span>';
        echo '</a></li>';
    }
    echo '</ul>';
}
1
  • Had to insert a semi colon after $counter = 1, but other than that it worked perfectly. This will be very helpful in the future, thank you
    – Phill
    Jul 29, 2012 at 8:08

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