While loop: Output something different on every second result

Posted by Wade D Ouellet on Stack Overflow See other posts from Stack Overflow or by Wade D Ouellet
Published on 2010-05-11T01:44:13Z Indexed on 2010/05/11 1:54 UTC
Read the original article Hit count: 372

Filed under:
|
|
|

Hi, I am running a plugin called Category Posts Widget for WordPress: http://wordpress.org/extend/plugins/category-posts/

It uses a while loop to display the names of all posts in a certain category. I want to get it so that there is a different class attached to the li tag on every second output.

Here is the block of code for the plugin:

// Post list
    echo "<ul>\n";

    while ( $cat_posts->have_posts() )
    {
        $cat_posts->the_post();
    ?>
        <li class="cat-post-item">
            <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

            <?php
                if (
                    function_exists('the_post_thumbnail') &&
                    current_theme_supports("post-thumbnails") &&
                    $instance["thumb"] &&
                    has_post_thumbnail()
                ) :
            ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
                </a>
            <?php endif; ?>

            <?php if ( $instance['date'] ) : ?>
            <p class="post-date"><?php the_time("j M Y"); ?></p>
            <?php endif; ?>

            <?php if ( $instance['excerpt'] ) : ?>
            <?php the_excerpt(); ?> 
            <?php endif; ?>

            <?php if ( $instance['comment_num'] ) : ?>
            <p class="comment-num">(<?php comments_number(); ?>)</p>
            <?php endif; ?>
        </li>
    <?php
    }

    echo "</ul>\n";

I am just trying to get it so on each second one in the output list, the li has a different class, so cat-post-item-alt for example.

Thanks,

Wade

© Stack Overflow or respective owner

Related posts about php

Related posts about while-loops