Wildflowers Wild wood Dog food Fox in repose Wasp's nest

1st May, 2004

Ramblings about markup

Filed under: WordPress, — bsag @ 05:05 PM

You might notice that some of the comments in the list below each post now have white backgrounds, and others have grey backgrounds. There’s a story behind this. I wanted to have a fancy alternating colour scheme for the comments, so that they would have alternating white and grey backgrounds1. Fine, I thought—this should be easy. Each comment has a comment ID number, so if I write a function to check whether the ID is even or odd, I can set the class attribute of the li element to ‘even’ or ‘odd’ appropriately. Sorted.

Or not. I had forgotten that the comment IDs are assigned as each comment is made over the whole site, so that the numbers on the comments on each post are not necessarily consecutive. So instead of a nice orderly alternation, I have a distressing degree of randomness. I’m sure that there must be a way to do this properly, using the content() attribute of lists, but I can’t think how. Does anyone know how, or can you point me to a helpful tutorial on the subject?

1 In the memorable words of Pete, the yellow was a bit jingly.

  1. 1

    If only all browsers supported CSS3's nth-child selectors eh? wink

    I'm not familiar with WordPress [still sticking with MT for now] but presumably it has a section of the comments template that loops over the comments. You should just be able to set up your own counter in the template. Outside the foreach ($comments as $comment) loop, you'd set $commentcount = 0; within the foreach loop you'd increment the counter [$commentcount = $commentcount+1;] then where appropriate you'd have class="<?php if (isint($comment_count/2)) {echo 'class="background1"'} else {echo 'class="background2"'} ?>".----- hmmm... i like some randomosity, myself... the comments are still a bit difficult for me to read, but not as bad as at first...

    by stacy @ 01/05/2004 10:06 pm • Permalink

  2. 2

    David: Gah! Why didn't I think of that! Yes, that should work fine. I'll give it a go later.

    stacy: You could try increasing the font size in your browser--that should help.

    by bsag @ 02/05/2004 7:05 am • Permalink

  3. 3

    The other way, following on from David's comment, would be an if/then, i.e. "if ($commentcount=="odd") { $commentcount="even" } else { $commentcount="odd" }

    then do class="$commentcount"

    Just another variant of a theme, I'm sure.

    by Lyle @ 04/05/2004 12:06 pm • Permalink

  4. 4

    Lyle: Yep, that would work. I had to wrack my brains for a moment to see how it would work, then I realised that it was rather subtle and elegant—nice! By the way, the last section of code got mangled by the HTML parser, so I had to edit and simplify it a bit. I think it still represents what you meant.

    by bsag @ 04/05/2004 5:06 pm • Permalink

  5. 5

    Yeah, it does. Sorry, I seem to have an innate skill in breaking your HTML parser. Call it crash-testing. grin

    by Lyle @ 05/05/2004 8:06 am • Permalink

blog comments powered by Disqus

Powered by ExpressionEngine :: © www.rousette.org.uk, 2002-2008 :: [XHTML] [CSS] [508]