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.
[…] See original here: WordPress Alternate comment colors – wpsnipp […]