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.

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

share|improve this question

marked as duplicate by toscho Jul 29 '12 at 10:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer 1

up vote 2 down vote accepted

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>';
}
share|improve this answer
    
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 '12 at 8:08
add comment

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