WordPress plugins, themes, tips and hacks

Using PHP to alternate background colors for comments or posts in WordPress

December 26, 2007 – 5:05 pm | by

Some blogs have alternating background colors for their comments and posts, i.e. a green background for every even comment and post, and a grey background for every odd comment and post. How do they do that?

Alternating Comments Styles

To alternate comments, add the following code to your comments.php template file, under where it says <?php if ( $comments ) : ?>:

<ul>
<?php $i = 0; ?>
<?php foreach ($comments as $comment) : ?>
<?php $i++; ?>
<li id="comment-<?php comment_ID() ?>"<?php if($i&1) { echo 'class="odd"';} else {echo 'class="even"';} ?>>

Then, create a style called .even and a style called .odd in your style.css file. Give each style a different background color, like this (but change the colors to what you want):

.odd {
background-color: #fcf9fc; }

.even {
background-color: #616161; }

This information is from the following WordPress support topic:

How to alternate background colors of comments posted on blog?

Alternating Post Styles

Adam Brown has a tutorial on his site for creating alternating styles for posts. Basically, you do the following:

  1. Right before the loop begins, add the following code:
    <?php $odd_or_even = 'odd'; ?>
  2. Right after the loop, there will be a div. Let’s say it’s called class=”post”. Change it to
    <div class="post <?php echo $odd_or_even; ?>">, and then add the following piece of code on the next line: <?php $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd'; ?>
  3. Now, create .odd and .even styles in your style.css.

Adam’s site has another really useful PHP Tutorial for Wordpress Users, which is good for those of us who aren’t going to become PHP gurus, but would like to know enough PHP to extend our WordPress sites.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
  1. 13 Responses to “Using PHP to alternate background colors for comments or posts in WordPress”

  2. By Forrest on Jan 1, 2008 | Reply

    Depending on your theme, it can be nice to use a gradient background image instead of a solid bg color for this.  The beautiful thing is once you’ve assigned them classes, you can go back and fine tune the appearance in CSS at any time.

  3. By John Doe on Jul 25, 2008 | Reply

    Used this and worked great, just took me time to realize I had to replace my old stuff with it haha.

  4. By Fred on Nov 17, 2008 | Reply

    WOW! Thanks!

  5. By Sean C on Apr 17, 2009 | Reply

    Awesome post! This has really helped. Thanks

  6. By Kangourou pas sympa on Apr 27, 2009 | Reply

    Does “Alternating Comments Styles”  work with wordpress 2.7 ?
    Because I tried without succes.
    Thank you in advance.

  7. By Ryan on May 1, 2009 | Reply

    @Kangourou pas sympa – Yep, it works with 2.7.

  8. By Eligio on May 9, 2009 | Reply

    I also want to know how to do it with wp2.7. thanks

  9. By Voya on Jun 4, 2009 | Reply

    Just tested – works sweet with 2.7 :)

    Thanks a million!

  1. 5 Trackback(s)

  2. Dec 26, 2007: PHP Coding School » Blog Archive » php tutorial [2007-12-26 15:34:13]
  3. Dec 30, 2007: Buffer Dump 29DEC07 « Feet up, eyes closed, head back
  4. May 9, 2008: Come alternare i colori di sfondo dei commenti nei blog Wordpress - Geekissimo
  5. Jun 3, 2008: Get to know every line of your comments.php WordPress theme file | WordPressGarage.com
  6. Nov 9, 2008: How to: Alternate background color in comments list

Post a Comment

Revolution Premium Themes