You should add the following code in functions.php
:
add_filter ( 'post_class' , 'my_post_class' );
global $current_class;
$current_class="odd";
function my_post_class ( $classes ) {
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == 'odd') ? 'even' : 'odd';
return $classes;
}
This ensures that all the odd posts on the page will have the class ‘odd’ and all the even posts will have the class ‘even’ just by using post_class()
in your theme.