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
The following is untested, but it illustrates the basic principle. Just switch a boolean value on every instance of the loop. Even posts will additionally have the class
cat-post-item-even
, so that you won’t have to modify your stylesheet more than necessary.It’s neat to discover that for loops can be used for things other than simple increments.
The two edited lines are marked as such.