Add ‘even’ and ‘odd’ class to the WordPress post_class()

You might find the need to have alternate ‘odd’ and ‘even’ styles for your posts. While you can define these as needed on the front end, I find it a whole lot more useful to define a function that adds these alternating classes to the post_class() function. Its quick, easy and painless.

To make sure that when you write <div <?php post_class(); ?>, the post/entry divs will be given the alternating odd/even CSS classes, add the following to the functions.php:

add_filter ( 'post_class' , 'butter_odd_or_even' );
if( !function_exists( 'butter_odd_or_even' ) ) {
  global $current_class;
  $current_class = 'odd';
  function butter_odd_or_even ( $classes ) {
    global $current_class;
    $classes[] = $current_class;
    $current_class = ($current_class == 'odd') ? 'even' : 'odd';
    return $classes;