Create Odd and Even Wordpress Posts

Like how you can change the background colors of odd and even Wordpress comments? Here’s how you do it for your posts too.

Have you checked out my Twitter category page? I feed all my twitter.com updates into there and have styled the page a certain way so to suit the content.

One thing I’ve recently done is switch the background colors depending on whether an entry is ‘odd’ or ‘even’. This just means they alternate, making them easier to distinguish from one another. Here’s how you do this easily:

Replace have_posts with this in the loop:

<?php
$x=0;
if (have_posts()) : ?>

Add a check and condition for odds and evens where your content for each post begins:

<?php if ($odd = $x%2){?>
<div class="odd"> 
<?php } else { ?><div class="even"> <?php } ?>

Make sure you close those div tags after your post content.

Then replace the endwhile in your loop with this:

<?php
$x++;
endwhile; ?>

Now you can go CSS crazy changing the background, colors, sizes for each alternate post!

Tags: ,

This entry was posted on Monday, April 21st, 2008 at 3:37 am and is filed under Long. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

ss_blog_claim=53530d43dc267adc556cf944bc3e7d55