WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
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

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

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

Last updated on July 14th, 2016 by Editorial Staff
219 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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.

219 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

16 Comments

Leave a Reply
  1. Sarmad Gardezi says:
    Jan 23, 2020 at 6:49 am

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

    Reply
    • WPBeginner Support says:
      Jan 23, 2020 at 9:10 am

      It should automatically affect custom post types :)

      Reply
  2. Vera says:
    Mar 30, 2019 at 6:32 am

    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

    Reply
    • WPBeginner Support says:
      Apr 1, 2019 at 11:52 am

      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 :)

      Reply
  3. Oliur says:
    Jul 16, 2016 at 11:10 pm

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

    Reply
    • WPBeginner Support says:
      Jul 17, 2016 at 10:25 pm

      Please see our guide on how to switch from Blogger to WordPress.

      Reply
  4. kaluan says:
    Jul 15, 2016 at 5:02 am

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

    Reply
  5. onkar says:
    Jul 27, 2015 at 3:30 am

    how to add odd even class in posts for particular page

    Reply
  6. Bucur says:
    Feb 19, 2014 at 3:19 am

    Ok good function, but css style?

    . post {
    / / Rest of the css
    }

    .odd { } ???

    Reply
  7. Simon says:
    Jan 7, 2014 at 2:03 pm

    Thanks for this. Just what I needed.

    Reply
  8. Samuel says:
    Mar 21, 2013 at 7:54 pm

    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!

    Reply
    • Editorial Staff says:
      Apr 1, 2013 at 8:53 am

      You would have to use WordPress conditionals:

      http://codex.wordpress.org/Conditional_Tags

      Reply
  9. Eric says:
    Dec 27, 2012 at 11:20 pm

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

    Reply
  10. Christine says:
    Sep 4, 2012 at 8:50 am

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

    Reply
  11. Daniele Zamboni says:
    May 10, 2012 at 1:34 am

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

    Reply
    • wpbeginner says:
      May 10, 2012 at 8:10 am

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

      Reply

Leave a Reply Cancel 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
OptinMonster
OptinMonster
Convert website visitors into email subscribers. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2021 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2021)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2021)
    • SiteGround Reviews from 4464 Users & Our Experts (2021)
    • Bluehost Review from Real Users + Performance Stats (2021)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2021 – Step by Step Guide
Deals & Coupons (view all)
AliDropship Coupon Code
AliDropship Coupon
Get 15% off on AliDropship WordPress and WooCommerce dropshipping plugin.
PremiumCoding
PremiumCoding Coupon
Get 40% off on PremiumCoding's collection of beautiful WordPress themes.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
  • Growth Fund
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon
  • AIOSEO

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress Security by Sucuri.