Forums

How to- alternating comment background colors (6 posts)

  1. jessn
    Member
    Posted 2 years ago #

    I've found a lot of articles about alternating comment backgrounds in an even/odd fashion. But I was wondering how to do it for more than two background colors/images. I have six different background images that I'd like to use for the comments, each new comment cycling through the images in order. Surely there's a way to do this, isn't there?

    I'd appreciate any advice!

  2. monkeymynd
    Member
    Posted 2 years ago #

    You would do it the same way as the even/odd method...except, instead of seeing if the number is even/odd, you need to use the "modulus" (%) operator with the number 6. "modulus" returns the remainder after one number is divided by another number.

    So, you would loop through your posts, incrementing a $count variable.

    Then, when you're ready to set the bg color, you would use a switch statement:

    switch ($count % 6) {
        case 0:
            // do stuff for sixth background here
            break;
        case 1:
            // do stuff for first background here
            break;
        case 2:
            // do stuff for second background here
            break;
        case 3:
            // do stuff for third background here
            break;
        case 4:
            // do stuff for fourth background here
            break;
        case 5:
            // do stuff for fifth background here
            break;
    }
    ?>
  3. jessn
    Member
    Posted 2 years ago #

    Wow. You're awesome! Here's a bit of PHP I found that alternates the comments on an even/odd basis.

    <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"';} ?></ul>

    How do I integrate your example?

  4. monkeymynd
    Member
    Posted 2 years ago #

    Something like the code below should work. Where it says, your_class_here you would put in the class name you want for each of the six backgrounds. Leave the quotes around it. So, an example would look like:

    echo 'class="red"';

    or

    echo 'class="blue"';

    For red background or blue background, but whatever your classes are named.

    <ul>
    <?php
      $i = 0; ?>
      foreach ($comments as $comment) :
      $i++;
    ?>
    <li id="comment-<?php comment_ID() ?>"
      <?php switch ($i % 6) {
        case 0:
            echo 'class="your_class_here"';
            break;
        case 1:
            echo 'class="your_class_here"';
            break;
        case 2:
            echo 'class="your_class_here"';
            break;
        case 3:
            echo 'class="your_class_here"';
            break;
        case 4:
            echo 'class="your_class_here"';
            break;
        case 5:
            echo 'class="your_class_here"';
            break;
      }
      ?></ul>
  5. jessn
    Member
    Posted 2 years ago #

    I love you! Thanks so much, I can' wait to try it out!

  6. hannarms
    Member
    Posted 2 years ago #

    sorry for bumping this really old thread. i've been searching all over the net for something like this and couldn't find anything else. i thought i'd just reply on this thread instead of making a new one..

    i was planning to automatically insert an image on post, i have 10 images, and i was hoping to automatically post 1 image per post, so if i have 10 posts and all images was already used, the first image will be use again for the 11th post..

    so i saw this thread and i thought i might work.
    like instead of using "background colors", i would use background image and place it on the left side of the post. will it work? if so, can someone please guide me how to do this? i saw the codes above but i don't know where to put them and all :/

    appreciate any help.

    thank you.

Topic Closed

This topic has been closed to new replies.

About this Topic