X

How to Allow Alternate Comment Colors in WordPress

Snippets by IsItWP

Are you looking for a way to show alternate colors in your comments? While there’s probably a plugin for this, we have created a quick code snippet that you can use to allow alternate comment colors in WordPress.

Instructions:

All you have to do is add this code to your theme’s comments.php file:

<?php if ( $comments ) : ?>:

<ul>
<?php 
          $i = 0; 
          foreach ($comments as $comment) :
          $i++; 
?>

<li id="comment-<?php comment_ID() ?>"<?php if($i&1) { echo 'class="odd"';} else {echo 'class="even"';} ?>>

Add this code in your CSS file:

Note: You can change the colors. Odd represents the first comment and even represents the second comment.

.odd  {background: #e5e5e5; }
.even {background: #ccc; }

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 8 best WordPress review plugins and how to secure WordPress forms.

Comments   Leave a Reply

  1. wp-popular.com » Blog Archive » Wordpress Alternate comment colors – wpsnipp November 17, 2010 at 12:44 am

    […] See original here: WordPress Alternate comment colors – wpsnipp […]

Add a Comment Cancel reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!