• Home
  • About
  • Contact
Blue Orange Green Pink Purple

Alternating wordpress comment styles

Posted in PHP, Tips, Wordpress. on Saturday, November 29th, 2008 by Matt
Nov 29

I found this little trick a while ago which alternates the style of your wordpress comments. I’m basing this tip off of the default wordpress comments.php file, but it shouldn’t be too different for any other theme. The default comments.php file actually comes with a built in solution for alternating comment styles, but I was never fond of their way of doing it, so I looked for a different way.

First, locate the following line of code in the comments.php file:

<?php foreach ($comments as $comment) : ?>

Before that line add:

<?php $i = 0; ?>

and after it add:

<?php $i++; ?>

Below the pieces of code you just added, you should see a list item tag (<li>) that looks something like this:

<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>">

Replace that line of code with this:

<li class="<?php echo($i&1)?'oddBg':'evenBg' ?>

Now you can open up your stylesheet and add styles to the oddBg and evenBg classes.

You do this same effect for your wordpress posts, but I’ll leave that up to you to figure out.

If you have any questions or suggestions, feel free to leave a comment or send me an email.

Leave a Reply

Matt Pavia

RSS Feed
  • About
    Hello, my name is Matthew Pavia. I currently live in New York and I am a sophomore in high school. My world revolves around technology, be it web design, programming, hardware, software or anything in between. I created this blog for a place for me to talk. To find out more about me, visit my about page.
  • Recent Articles
    • Happy holidays!
    • Different Kind of Christmas Poem
    • Cliff Young
    • New Theme
    • Alternating wordpress comment styles
    • New Domain
  • Categories
    • General
    • Holidays
    • Inspirational
    • Other
    • PHP
    • Site News
    • Tips
    • Wordpress
  • Archives
    • December 2008
    • November 2008
  • Search



RSS Feed


  • Home
  • About
  • Contact

Copyright © 2009 Matthew Pavia.   Valid xHTML & CSS.

Designed by FTL Wordpress Themes.

Back to Top