Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Add Odd/Even Class to Your Post in WordPress Themes

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Add Odd/Even Class to Your Post in WordPress Themes

Do you want to add Odd and Even classes to posts in your WordPress theme? Adding an odd and even class allows you to style every other post differently. In this article, we will show you how to add odd/even class to your post in WordPress themes.

Adding Odd/Even class to your posts in WordPress themes

Why Add Odd/Even Class to Your Posts in WordPress Themes?

Many WordPress themes use an old or even class for WordPress comments. It helps users visualize where one comment ends and the next one begins.

Similarly, you can use this technique for your WordPress posts. It looks aesthetically pleasing and helps users quickly scan pages with lots of content. It is particularly helpful for homepage of magazine or news websites.

Having said that, let’s see how to add an odd and even class to your posts in WordPress theme.

Adding Odd/Even Class to Posts in WordPress Theme

WordPress generates default CSS classes and adds them to different items on your website on the fly. These CSS classes help plugin and theme developers add their own styles for different items.

WordPress also comes with a function called post_class, which is used by theme developers to add classes to post item. See our guide on how to style each WordPress post differently.

The post_class is also a filter, which means you can hook your own functions to it. This is exactly what we will be doing here.

Simply add this code to your theme’s functions.php file or a site-specific plugin.

function oddeven_post_class ( $classes ) {
   global $current_class;
   $classes[] = $current_class;
   $current_class = ($current_class == 'odd') ? 'even' : 'odd';
   return $classes;
}
add_filter ( 'post_class' , 'oddeven_post_class' );
global $current_class;
$current_class = 'odd';

This function simply adds odd to the first post, then even, and so on.

You can find the odd and even classes in your site’s source code. Simply take mouse to a post title and then right click to select Inspect or Inspect Element.

Odd and Even classes in source code

Now that you have added even and odd classes to your posts. The next step is to style them using CSS. You can add your custom CSS to your child theme’s stylesheet, or by using Simple Custom CSS plugin.

Here is a sample CSS that you can use as a starting point:

.even {
background:#f0f8ff;  
} 
.odd {
 background:#f4f4fb;
}

This is how it looked on our test site:

Posts using alternate background colors with even/odd css classes in WordPress

If you don’t know how to use CSS, then you may want to check out CSS Hero. It allows you to add CSS to any part of your WordPress site without writing any code.

We hope this article helped you learn how to add odd/even class to your posts in WordPress themes. You may also want to see our guide on how to style your WordPress comments layout.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.


Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Oliur says:

    My Blog Name WayTrick. It a Blogger Blog. Now I want to my blog Tansfer to wordpress. How do it?

  2. kaluan says:

    This seems not working in Genesis framework? Any additional code needed to add?

  3. onkar says:

    how to add odd even class in posts for particular page

  4. Bucur says:

    Ok good function, but css style?

    . post {
    / / Rest of the css
    }

    .odd { } ???

  5. Simon says:

    Thanks for this. Just what I needed.

  6. Samuel says:

    How could I take this a step further and target a specific post category?

    I have a custom post type – testimonial, and I only want odd/even styling in that section.

    Thanks!

  7. Eric says:

    This is by far the easiest method of creating odd and even posts for wordpress!! Thanks so much for sharing!

  8. Christine says:

    As, sadly, css3 selectors are not well supported by all browsers…

    I just tried your code for a new twenty eleven child theme i’m customizing, it works so fine,

    Thanks a Lot for sharing this ! !

  9. Daniele Zamboni says:

    What is odd/even class ? Sorry but i’m newbie

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.