» WordPress Coding & Design » Alternating Comment Styles

SarahAlternating Comment Styles

Written by Sarah from Stuff By Sarah on June 1, 2008

Following on from last weeks post on highlighting your comments, I figured another useful post would be about setting up your comments to have alternating styles. We do this by adding an additional CSS class to every other comment, then you can style this class in your stylesheet with, perhaps, a different background colour, a border, or even put the user’s gravatar on the opposite side of the comment to normal.

To work out which comments to alternate we use a basic PHP “if statement” to determine which comment is being displayed in the comment loop. To do this use a boolean (TRUE/FALSE) variable in the loop i.e.

if ($altcom) : $altcom = FALSE; else : $altcom = TRUE; endif;

This code checks if the boolean variable $altcom is TRUE. If it is then it sets the value to FALSE, if it’s FALSE it sets the value to TRUE. This gives us the alternating variable, which we can then use to control the output of a CSS class to every other comment in the loop.

Using the Classic theme’s comment.php file as an example, to add the above code in we change the following lines:

<?php foreach ($comments as $comment) : ?>
<li id="comment-<?php comment_ID() ?>">

to

<?php $altcom = TRUE;
foreach ($comments as $comment) :
if ($altcom) : $altcom = FALSE; else : $altcom = TRUE; endif;
?>
<li id="comment-<?php comment_ID() ?>"<?php if ($altcom) echo ' class="altcomment"'; ?>>

The first line declares our boolean variable as TRUE. This will just avoid any PHP warnings/errors. The foreach line is the start of the comment loop, then the if statement goes in to give us the alternating variable. As you can then see we’ve used this variable then to control whether our class is output or not on the last line.

Then you just need to add a new class to your stylesheet to style the alternative comment differently. Put this after your comment styling is declared so that the alternative comment style overrides the default one.

If you already have a class in your comment wrap (this could be a list item, a div or something else) then alter the last line of the above code to just echo out the second class name ie.

<?php if ($altcom) echo ' altcomment'; ?>

If you implemented the author highlight code from last week then you may want to merge these two pieces of code. If so then add a class to your comment wrap if one doesn’t already exist and then run the two if statements, one after the other ie.

<li id="comment-<?php comment_ID() ?>" class="<?php if ($altcom) echo 'altcomment '; if ($comment->user_id == get_the_author_ID()) echo 'mycomment'; ?>">

This will then output either or both classes when relevant.

Don’t forget, the list item code above is just an example from the Classic theme. Your comments.php file may be different and use a div or other element, covering all possibilities would take me a while, so if you’re not sure then leave a comment with the code you think is relevant and I’ll let you know where to add this code.

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • Bumpzee
  • E-mail this story to a friend!
  • Ma.gnolia
  • Print this article!
  • Reddit
  • StumbleUpon
  • Technorati
  • TwitThis
Written by Sarah from Stuff By Sarah on June 1, 2008 | Filed Under WordPress Coding & Design
Unique Blog Designs

7 Responses so far | Have Your Say!

  1. cush  |  June 16th, 2008 at 6:45 pm #

    cush - Gravatar

    HI

    How exactly do i change my html so that i can have the same comment as yours.

    ie, no (or number) responses - have your say.

    I like this as it is bold and doesnt take up too much room.

    At the moment my html is this:oops: :?:



     

  2. cush  |  June 16th, 2008 at 6:45 pm #

    cush - Gravatar

    oh sorry it didnt show the html

  3. Sarah (Post Author)   |  June 17th, 2008 at 2:03 pm #

    Sarah - Gravatar

    Hi cush, it’s a bit hard to tell without seeing the full code. Your best option is to either put the code into a text file and link to it from here or join the blogging tips forum and post the code in there and someone will help you with it then :)

  4. cush  |  June 19th, 2008 at 12:12 am #

    cush - Gravatar

    I cant seem to put the html code in here, so if i put my website and you have time, could you have a look and see.

    its http://godontherun.blogspot.com

    I think you will see the comments section is ugly and not user friendly and takes up too much room.

    I just want something simple, that also encourages people to comment ( there are probably a million ways out there, but i am not too fussy lol).

    Thanks so much for your help.

    ALso, do you know how i can widen the left hand side of my blog so that the writing room is larger?

    Thanks
    Cush

  5. Sarah (Post Author)   |  June 19th, 2008 at 3:07 am #

    Sarah - Gravatar

    Hi Cush, unfortunately I don’t know much about Blogger as I use WordPress. Your best option is to probably post on the forums here where there are a few Blogger users who may be able to help you out :)

  6. cush  |  June 19th, 2008 at 5:12 am #

    cush - Gravatar

    Ok thanks for that, i will. :)

  7. WINDOWS  |  June 26th, 2008 at 8:33 pm #

    WINDOWS - Gravatar

Trackbacks to 'Alternating Comment Styles'

Leave Feedback

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>