Many times i have looked for a simple solution to alternate the background colors on my wordpress post listings. Here is a simple solution to get the job done.
1) Create a variable and set it to 0 before the post’s while statement
<?php $alternate = 0; while (have_posts()) : the_post(); ?>
2) Then with in the while statement create an if statement as such and place it on you desired container
<div <?php
if($alternate == 0){ echo "class='style1'";}else{ echo "class='style2'";}
?>>
3) Add this snippet of code somewhere else in the while statement
<?php $alternate = 1-$alternate; ?>
Now to explain. The first post placed $alternate equals 0, then it will equal 1 minus $alternate (which is zero) so now $alternate equals 1. The second post placed equals 1 minus 1 which results as 0. Therefore it will alternate the styles that are placed in the if statement.
There are many other ways to accomplish this out there, if you know of one post it below.
Tags: alternating post styles, Tutorials, Wordpress, Wordpress Template





