• Hello all;
    This is my FIRST HACK! What I did with my b2 blog is format the comments so that they were one on top of the other, but… I made it so that the background colors alternated for easy readability. I had modified this slightly to work with WordPress’s b2comments.php.
    skeen’s alternating background hack
    Below the first line of b2comments.php, add the following:
    $bgColor = "#EEEEEE";
    You would now use the $bgColor variable as the colour you wish to change your background to. Here is an example of what I did:
    <span style="background-color:<?=$bgColor;?>;">
    <?php comment_text() ?>
    </span>

    Below this, add the following code:
    <?
    if("#EEEEEE" == $bgColor) {
    $bgColor="#DBDBDB";
    }
    else {
    $bgColor="#EEEEEE";
    }
    ?>

    Thats it! You’re set. Your comments background color will now alternate.

Viewing 5 replies - 16 through 20 (of 20 total)
  • None taken. 😉
    Just so you are clear, that array is an array of NAMES, not color values. Those names are the CSS class names for the given color blocks. The advantage being that you can make the array as big as you want, index into it however you want, not playing with the actual class names within some tighter code lower in the page. Cleaner to say the least.
    The other thing it does is allows you to do things like add a field to the categories table, so that each category also has a color-class-tagging. At the top of the page, you extract those color tags into an array indexed by the category ID, and then within the posts loop you pickup the proper class for the post’s category. Works well for single-category posts.. 😉
    However, this IS where you might also want something more database-driven. Maybe the colors themselves should be specified in the category table or something. Depends on each person’s needs — some people may desire the explicitly database-driven control rather than CSS. Some people might not even want to touch the CSS, in which case programmatic building is better. Not to leave out CSS, you can also programmatically >build< a set of styles on the fly at the top of your page, based on the database — combined data-driven and CSS-applied stylings.
    Lots of ways to skin this cat! 😉
    -d

    Hm, I see, my fault… I thought color1, color2 etc refer to the hex values of colors, not php variables. *Ashamed* :p
    And I prefer to use CSS for styling/layout, that’s what it’s made for 😉 DB used for storing content. Once again, my apologies 🙂

    No problem! Yeah, I think there’s a lot of different solutions here that depend on how people like to use php/html/css/sql. For instance, my CG-Amazon library depends highly on web-side cache files with a lightweight table for larger querying — no reason to ask for large amounts of data you don’t need, or to slow your page down by bogging down the SQL server with too many requests. 😉
    -d

    Cine, just tried your method! fantastic, works like a charm. 😀

    That was fantastic Cine. For some reason the other methods weren’t working for me and like said above I wanted more than two colours and that bit of code made it really simple!!

    Thank You if you still come by here!

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘Alternating comment backgrounds’ is closed to new replies.