Different class to every second post in WordPress blog

I researched a whole web, and the solution for assigning different class to every 2nd post in blog view in WordPress was adding below code into function.php. Most people claim it is helpful, but to me it just adds odd at the end of the post class. There is no even class anywhere.

function alternating_post_class($classes) {
    global $wp_query;
    $classes[] = ( $wp_query->current_post%2 === 0 ? 'odd' : 'even' );
    return $classes;
}
add_filter('post_class', 'alternating_post_class');

What have I done wrong? It is suposed to add odd class to my first blog post, then even class for my second post in the blog view, but the result is as I said always the same: odd.

Related posts

Leave a Reply