Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

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

Want to add a touch of visual interest to your WordPress blog posts? Adding odd and even classes can help you achieve a unique and engaging design by styling every other post differently.

We frequently receive questions from our readers about how apply this technique. While many WordPress themes don’t offer a built-in option for odd and even post classes, it’s a relatively simple process to add them yourself.

In this article, we’ll 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 odd 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 the 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, in a site-specific plugin, or in a code snippets 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';

We recommending adding this code using the WPCode plugin. It’s the best code snippets plugin that makes it safe and easy to add custom code in WordPress.

WPCode

To begin, you need to install and activate the free WPCode plugin. For instructions, see this guide on how to install a WordPress plugin.

Upon activation, go to the Code Snippets » + Add Snippet page from the WordPress dashboard. Then, click the ‘Use Snippet’ button under the ‘Add Your Custom Code (New Snippet)’ option.

Add a new custom code snippet in WPCode

From there, add a title for your code snippet, which can be anything to help you remember what the code is for.

Next, paste the code from above into the ‘Code Preview’ box and select ‘PHP Snippet’ as the code type from the dropdown list on the right.

Paste code snippet into WPCode plugin

After that, simply switch the toggle from ‘Inactive’ to ‘Active’ and click the ‘Save Snippet’ button.

Activate and save your custom code snippet

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, the theme customizer, or by using the WPCode 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 our guide on how to easily add custom CSS to your WordPress site.

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 and our expert picks of the best WordPress page builders.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

16 CommentsLeave a Reply

  1. Sarmad Gardezi

    I want to use it with CPT then how i can use it ?

    • WPBeginner Support

      It should automatically affect custom post types :)

      Admin

  2. Vera

    Hello,
    I was trying to make this work on my test site, where I work with Elementor and Astra. For some reason, once I add the code – everything colors with the “odd” color, and I don’t understand why.
    Can you help me sort it out, please?
    Vera

    • WPBeginner Support

      For that question, you would want to check with Elementor to ensure their page builder is not overriding the function and they should be able to assist :)

      Admin

  3. Oliur

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

  4. kaluan

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

  5. onkar

    how to add odd even class in posts for particular page

  6. Bucur

    Ok good function, but css style?

    . post {
    / / Rest of the css
    }

    .odd { } ???

  7. Simon

    Thanks for this. Just what I needed.

  8. Samuel

    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!

  9. Eric

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

  10. Christine

    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 ! !

    • wpbeginner

       @Daniele Zamboni These are CSS classes that you can add for styling purposes.

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.