• I using wordpress to implement a classifieds site. I have the theme set up to display only the post titles on the home page.

    Right now I have the post title in a div & the whole div is a clickable link. I can’t work out how to change the background colour of the div alternating for each post title to achieve a zebra table effect.

    Example: https://www.jobber.ro/ – If possible without using any javascript.

    Since the Get_Posts loops. I think I need to do some PHP magic but I am unsure where to begin. Any help would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Just add a little code inside your loop: (this is a bit verbose for clarity)

    <?php
    if ($alt) {
       $alt = "";
    } else {
       $alt = "altclass";
    }
    ?>
    <div class="alreadyclass <?php echo $alt; ?>"><?php the_title(); ?></div>
    ...

    Every odd loop, $alt will be “altclass”. Use that to zebra in your CSS.

    Roger,

    Thanks so much for posting this. I was trying to do the same, but over-egging it. Your solution is beautifully simple. ??

    You’re most welcome.

    <?php $alt = ($alt) ? "" : "altclass"; ?>

    is the less verbose method. But sometimes brevity has pitfalls.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Apply CSS Style to alternate post titles’ is closed to new replies.