As my tag pages grow I have been contemplating how I wanted to handle their appearance. The goal is to have as many post excerpts as possible on each tag page while creating a page that is easy to read. I decided to try something a little different that I have not seen on too many blogs, actually, I am not sure I have seen it at all except on some comments, which is alternating the background color of each post.
So, how hard would this be?
It took about an hour to get it the way I wanted it. I am alternating the background color and the Adsense ads on each tag page. A good example is the Google Tag Page since it has a lot of posts.

Here is a summary of how I did this:
Create a loop counter. Right before the Loop begins set a variable to zero.
<?php
$x=0;
if (have_posts()) : ?>
At this point I would want the Background Color to change, insert code to test for an even number post, and if this is true, add an inline CSS style for the background that would override the stylesheet.
<?php if ($odd = $x%2){?>
<div style=”background-color:#404040;padding:10px 0px 10px 10px;”>
<?php }?>
Then, where the background color should return to the original state, run the same test for the even numbered post and close the div tag set by the Inline Style.
<?php if( $odd = $x%2 ){?>
</div>
<?php }?>
The final step is to add 1 to the counter for each post, which is done at the end of the loop.
<?php
$x++;
endwhile; ?>
Not sure if anyone else likes this style, but it sure seems to make the excerpts easier to read for me. Please let me know what you think ?
Subscribe to CyberCoders RSS feed.


I use alternating colors for comments on many of my blogs, but I guess I never thought do so for posts.
Great coding tutorial!
[...] can truly be applied all over your blog. David of CyberCoder posted yesterday a great hack to alternate the background of your blog posts. Here are the steps you need to take: Create a loop counter. Right before the Loop begins set a [...]
[...] can truly be applied all over your blog. David of CyberCoder posted yesterday a great hack to alternate the background of your blog posts. Here are the steps you need to take: Create a loop counter. Right before the Loop begins set a [...]
@Kyle, Thanks, like you posted on your site, there are lots of places where a loop occurs in WP and the same idea can be applied.
Thanks, now thinking about using colors for my posts as well. Let you know once we get it working.
Nice! Alternating colors makes it easier to read because you can identify the beginning and end easier. Thatnk you for the short howto.
thank you
Let you know once we get it working.
making comments coloured is a very nice thing, but i think colouring posts is also going to be a fun
[...] can truly be applied all over your blog. David of CyberCoder posted yesterday a great hack to alternate the background of your blog posts. Here are the steps you need to [...]