• WPChina

    (@wordpresschina)


    I want to alternate the background color of my comments. For example, comment #1 is white, then #2 is grey, and #3 is white, and #4 is grey, etc…

    I found the CSS to do this, but since comments 1,2,3,4 are the same markup, they all have the same background color.

    I think I need to edit comments.php, right? What exactly should I do to make this change?

    Or am I doing this totally wrong and is there a simpler method?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Just have a look in Kubrick. The alt comments code is in there. Copy and paste. ??

    Thread Starter WPChina

    (@wordpresschina)

    wonderful! Works!
    tks!

    It does? Heck………..:)

    Thread Starter WPChina

    (@wordpresschina)

    well…. one small thing… I added the details to the css file and also to comments.php, but the first comment in the string is not affected by all this… the 2nd, 3rd, 4th, 5th, etc comments are accurately marked-up, but the very first comment seems unaffected by the code changes.. very very weird… ????

    Kubrick borkage ??

    Thread Starter WPChina

    (@wordpresschina)

    Any idea how to fix this? I am not a php expert, but the php appears fine and seems to SHOULD work on the first entry in each array….

    Out of left field I would hazard a guess that the array is not initialised at zero.

    Thread Starter WPChina

    (@wordpresschina)

    ahhhh, right that does make sense and I see what you mean.

    However, here is my code below, and I don”t see where the array is initialised…

    <?php foreach ($comments as $comment) : ?>
    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>"><cite><?php comment_author_link() ?></cite> Says:
    <?php if ($comment->comment_approved == '0') : ?>
    <em>Your comment is awaiting moderation.</em>
    <?php endif; ?>
    <br />
    <small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('e','',''); ?></small>
    <?php comment_text() ?>
    </li>
    <?php /* Changes every other comment to a different class */
    if ('alt' == $oddcomment) $oddcomment = '';
    else $oddcomment = 'alt';?>
    <?php endforeach; /* end for each comment */ ?>

    tks!

    Thread Starter WPChina

    (@wordpresschina)

    Any ideas how to start the array or initialize it to start at zero?

    there is no array.
    $oddcomment is initialised and assigned the starting value of ‘alt’ further up in the code, line 15

    /* This variable is for alternating comment background */
    $oddcomment = 'alt';

    Later on, the code just alternates between values ‘alt’ and ”

    Another quick way of accomplishing alternating 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 just use CSS to define your “odd” and “even” classes.

    Thaaank youuuu andymike for that last piece of coding!! *jumps around in a euphoric fit*

    Just what I’ve been trying to figure out for like 3 months! (Ok, my websearch skills seems to be as good as my php…lol).

    So a big thank you!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘How to alternate background colors of comments posted on blog?’ is closed to new replies.