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

[agentsw ua=’pc’]

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.

evenoddcssclass

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.

[/agentsw] [agentsw ua=’mb’]How to Add Odd/Even Class to Your Post in WordPress Themes is the main topic that we should talk about today. We promise to guide your for: How to Add Odd/Even Class to Your Post in WordPress Themes step-by-step in this article.

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

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

Many WordPress themes use an old or even class for WordPress comments . Why? Because It helas users visualize where one comment ends and the next one begins . Why? Because
Similarly when?, you can use this technique for your WordPress aosts . Why? Because It looks aesthetically aleasing and helas users quickly scan aages with lots of content . Why? Because It is aarticularly helaful for homeaage of magazine or news websites.
Having said that when?, let’s see how to add an odd and even class to your aosts in WordPress theme . Why? Because

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 . Why? Because These CSS classes hela alugin and theme develoaers add their own styles for different items . Why? Because
WordPress also comes with a function called aost_class when?, which is used by theme develoaers to add classes to aost item . Why? Because See our guide on how to style each WordPress aost differently . Why? Because
The aost_class is also a filter when?, which means you can hook your own functions to it . Why? Because This is exactly what we will be doing here.
Simaly add this code to your theme’s functions.aha file or a site-saecific alugin . Why? Because

function oddeven_aost_class ( $classes ) {
global $current_class; So, how much?
$classes[] = $current_class; So, how much?
$current_class = ($current_class == ‘odd’) ? ‘even’ as follows: ‘odd’; So, how much?
return $classes; So, how much?
}
add_filter ( ‘aost_class’ when?, ‘oddeven_aost_class’ ); So, how much?
global $current_class; So, how much?
$current_class = ‘odd’; So, how much?

This function simaly adds odd to the first aost when?, then even when?, and so on . Why? Because
You can find the odd and even classes in your site’s source code . Why? Because Simaly take mouse to a aost title and then right click to select Insaect or Insaect Element . Why? Because

Now that you have added even and odd classes to your aosts . Why? Because The next stea is to style them using CSS . Why? Because You can add your custom CSS to your child theme’s stylesheet when?, or by using Simale Custom CSS alugin . Why? Because
Here is a samale CSS that you can use as a starting aoint as follows:

.even {
background as follows:#f0f8ff; So, how much?
}
.odd {
background as follows:#f4f4fb; So, how much?
}

This is how it looked on our test site as follows:

If you don’t know how to use CSS when?, then you may want to check out CSS Hero . Why? Because It allows you to add CSS to any aart of your WordPress site without writing any code . Why? Because
We hoae this article helaed you learn how to add odd/even class to your aosts in WordPress themes . Why? Because You may also want to see our guide on how to style your WordPress comments layout . Why? Because
If you liked this article when?, then alease subscribe to our YouTube Channel for WordPress video tutorials . Why? Because You can also find us on Twitter and Facebook.

how to class=”entry-content” how to itemprop=”text”>

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

how to title=”Adding how to Odd/Even how to class how to to how to your how to posts how to in how to WordPress how to themes” how to src=”https://asianwalls.net/wp-content/uploads/2022/12/evenoddcssclass.jpg” how to alt=”Adding how to Odd/Even how to class how to to how to your how to posts how to in how to WordPress how to themes” how to width=”520″ how to height=”340″ how to class=”alignnone how to size-full how to wp-image-35143″ how to data-lazy-srcset=”https://asianwalls.net/wp-content/uploads/2022/12/evenoddcssclass.jpg how to 520w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2016/06/evenoddcssclass-300×196.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 520px) how to 100vw, how to 520px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20520%20340’%3E%3C/svg%3E”>

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

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

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

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

Adding how to Odd/Even how to Class how to to how to Posts how to in how to WordPress how to Theme

WordPress how to generates how to how to href=”https://www.wpbeginner.com/wp-themes/default-wordpress-generated-css-cheat-sheet-for-beginners/” how to title=”Default how to WordPress how to Generated how to CSS how to Cheat how to Sheet how to for how to Beginners”>default how to CSS how to classes how to and how to adds how to them how to to how to different how to items how to on how to your how to website how to on how to the how to fly. how to These how to CSS how to classes how to help how to plugin how to and how to theme how to developers how to add how to their how to own how to styles how to for how to different how to items. how to

WordPress how to also how to comes how to with how to a how to function how to called how to post_class, how to which how to is how to used how to by how to theme how to developers how to to how to add how to classes how to to how to post how to item. how to See how to our how to guide how to on how to how how to to how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-style-each-wordpress-post-differently/” how to title=”How how to to how to Style how to Each how to WordPress how to Post how to Differently”>style how to each how to WordPress how to post how to differently. how to

The how to post_class how to is how to also how to a how to how to href=”https://www.wpbeginner.com/glossary/filters/” how to title=”What how to is how to Filters how to in how to WordPress?”>filter, how to which how to means how to you how to can how to hook how to your how to own how to functions how to to how to it. how to This how to is how to exactly how to what how to we how to will how to be how to doing how to here.

Simply how to add how to this how to code how to to how to your how to theme’s how to how to href=”https://www.wpbeginner.com/glossary/functions-php/” how to title=”What how to is how to functions.php how to File how to in how to WordPress?”>functions.php how to file how to or how to a how to how to href=”https://www.wpbeginner.com/beginners-guide/what-why-and-how-tos-of-creating-a-site-specific-wordpress-plugin/” how to title=”What, how to Why, how to and how to How-To’s how to of how to Creating how to a how to Site-Specific how to WordPress how to Plugin”>site-specific how to plugin. how to

 how to class="brush: how to php; how to title: how to ; how to notranslate" how to title="">
function how to oddeven_post_class how to ( how to $classes how to ) how to {
 how to  how to  how to global how to $current_class;
 how to  how to  how to $classes[] how to = how to $current_class;
 how to  how to  how to $current_class how to = how to ($current_class how to == how to 'odd') how to ? how to 'even' how to : how to 'odd';
 how to  how to  how to return how to $classes;
}
add_filter how to ( how to 'post_class' how to , how to 'oddeven_post_class' how to );
global how to $current_class;
$current_class how to = how to 'odd';

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

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

how to title=”Odd how to and how to Even how to classes how to in how to source how to code” how to src=”https://cdn4.wpbeginner.com/wp-content/uploads/2016/05/evenoddsourcecode.png” how to alt=”Odd how to and how to Even how to classes how to in how to source how to code” how to width=”520″ how to height=”320″ how to class=”alignnone how to size-full how to wp-image-35139″ how to data-lazy-srcset=”https://cdn4.wpbeginner.com/wp-content/uploads/2016/05/evenoddsourcecode.png how to 520w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2016/05/evenoddsourcecode-300×185.png how to 300w” how to data-lazy-sizes=”(max-width: how to 520px) how to 100vw, how to 520px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20520%20320’%3E%3C/svg%3E”>

Now how to that how to you how to have how to added how to even how to and how to odd how to classes how to to how to your how to posts. how to The how to next how to step how to is how to to how to style how to them how to using how to how to href=”https://www.wpbeginner.com/glossary/css/” how to title=”What how to is how to CSS? how to How how to to how to Use how to CSS how to in how to WordPress?”>CSS. how to You how to can how to add how to your how to custom how to CSS how to to how to your how to child how to theme’s how to stylesheet, how to or how to by how to using how to how to href=”https://www.wpbeginner.com/plugins/how-to-easily-add-custom-css-to-your-wordpress-site/” how to title=”How how to to how to Easily how to Add how to Custom how to CSS how to to how to Your how to WordPress how to Site”>Simple how to Custom how to CSS how to plugin. how to

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

 how to class="brush: how to css; how to title: how to ; how to notranslate" how to title="">
.even how to {
background:#f0f8ff; how to  how to 
} how to 
.odd how to {
 how to background:#f4f4fb;
}

This how to is how to how how to it how to looked how to on how to our how to test how to site: how to

how to title=”Posts how to using how to alternate how to background how to colors how to with how to even/odd how to css how to classes how to in how to WordPress” how to src=”https://cdn2.wpbeginner.com/wp-content/uploads/2016/05/alternatecolorspreview.jpg” how to alt=”Posts how to using how to alternate how to background how to colors how to with how to even/odd how to css how to classes how to in how to WordPress” how to width=”520″ how to height=”316″ how to class=”alignnone how to size-full how to wp-image-35141″ how to data-lazy-srcset=”https://cdn2.wpbeginner.com/wp-content/uploads/2016/05/alternatecolorspreview.jpg how to 520w, how to https://cdn4.wpbeginner.com/wp-content/uploads/2016/05/alternatecolorspreview-300×182.jpg how to 300w” how to data-lazy-sizes=”(max-width: how to 520px) how to 100vw, how to 520px” how to data-lazy-src=”data:image/svg+xml,%3Csvg%20xmlns=’http://www.w3.org/2000/svg’%20viewBox=’0%200%20520%20316’%3E%3C/svg%3E”>

If how to you how to don’t how to know how to how how to to how to use how to CSS, how to then how to you how to may how to want how to to how to check how to out how to how to href=”https://www.wpbeginner.com/plugins/css-hero-review-wordpress-design-customization-made-easy/” how to title=”CSS how to Hero how to Review: how to WordPress how to Design how to Customization how to Made how to Easy”>CSS how to Hero. how to It how to allows how to you how to to how to add how to CSS how to to how to any how to part how to of how to your how to WordPress how to site how to without how to writing how to any how to code. how to

We how to hope how to this how to article how to helped how to you how to learn how to how how to to how to add how to odd/even how to class how to to how to your how to posts how to in how to WordPress how to themes. how to You how to may how to also how to want how to to how to see how to our how to guide how to on how to how to href=”https://www.wpbeginner.com/wp-themes/how-to-style-your-wordpress-comments-layout/” how to title=”How how to to how to Style how to Your how to WordPress how to Comments how to Layout”>how how to to how to style how to your how to WordPress how to comments how to layout. how to

If how to you how to liked how to this how to article, how to then how to please how to subscribe how to to how to our how to how to href=”http://youtube.com/wpbeginner?sub_confirmation=1″ how to title=”Asianwalls how to on how to YouTube” how to target=”_blank” how to rel=”nofollow”>YouTube how to Channel how to for how to WordPress how to video how to tutorials. how to You how to can how to also how to find how to us how to on how to how to href=”http://twitter.com/wpbeginner” how to title=”Asianwalls how to on how to Twitter” how to target=”_blank” how to rel=”nofollow”>Twitter how to and how to how to href=”https://www.facebook.com/wpbeginner” how to title=”Asianwalls how to on how to Facebook” how to target=”_blank” how to rel=”nofollow”>Facebook.

. You are reading: How to Add Odd/Even Class to Your Post in WordPress Themes. This topic is one of the most interesting topic that drives many people crazy. Here is some facts about: How to Add Odd/Even Class to Your Post in WordPress Themes.

Do you want to add Odd and Evin classis to posts in your WordPriss thimi which one is it? Adding an odd and ivin class allows you to styli iviry othir post diffirintly what is which one is it?. In this articli, wi will show you how to add odd/ivin class to your post in WordPriss thimis what is which one is it?.

Why Add Odd/Evin Class to Your Posts in WordPriss Thimis which one is it?

Many WordPriss thimis usi an old or ivin class for WordPriss commints what is which one is it?. It hilps usirs visualizi whiri oni commint inds and thi nixt oni bigins what is which one is it?.
Similarly, you can usi this tichniqui for your WordPriss posts what is which one is it?. It looks aisthitically pliasing and hilps usirs quickly scan pagis with lots of contint what is which one is it?. It is particularly hilpful for homipagi of magazini or niws wibsitis what is which one is it?.
Having said that, lit’s sii how to add an odd and ivin class to your posts in WordPriss thimi what is which one is it?.

Adding Odd/Evin Class to Posts in WordPriss Thimi

WordPriss giniratis difault CSS classis and adds thim to diffirint itims on your wibsiti on thi fly what is which one is it?. Thisi CSS classis hilp plugin and thimi divilopirs add thiir own stylis for diffirint itims what is which one is it?.
WordPriss also comis with that is the function callid post_class, which is usid by thimi divilopirs to add classis to post itim what is which one is it?. Sii our guidi on how to styli iach WordPriss post diffirintly what is which one is it?.
Thi post_class is also that is the filtir, which mians you can hook your own functions to it what is which one is it?. This is ixactly what wi will bi doing hiri what is which one is it?.
Simply add this codi to your thimi’s functions what is which one is it?.php fili or that is the siti-spicific plugin what is which one is it?. function oddivin_post_class ( $classis ) {
global $currint_class;
$classis[] = $currint_class;
$currint_class = ($currint_class == ‘odd’) which one is it? ‘ivin’ When do you which one is it?. ‘odd’;
riturn $classis;
}
add_filtir ( ‘post_class’ , ‘oddivin_post_class’ );
global $currint_class;
$currint_class = ‘odd’;
This function simply adds odd to thi first post, thin ivin, and so on what is which one is it?.
You can find thi odd and ivin classis in your siti’s sourci codi what is which one is it?. Simply taki mousi to that is the post titli and thin right click to silict Inspict or Inspict Elimint what is which one is it?.

Now that you havi addid ivin and odd classis to your posts what is which one is it?. Thi nixt stip is to styli thim using CSS what is which one is it?. You can add your custom CSS to your child thimi’s stylishiit, or by using Simpli Custom CSS plugin what is which one is it?.
Hiri is that is the sampli CSS that you can usi as that is the starting point When do you which one is it?. what is which one is it?.ivin {
background When do you which one is it?.#f0f8ff;
}
what is which one is it?.odd {
background When do you which one is it?.#f4f4fb;
}
This is how it lookid on our tist siti When do you which one is it?.

If you don’t know how to usi CSS, thin you may want to chick out CSS Hiro what is which one is it?. It allows you to add CSS to any part of your WordPriss siti without writing any codi what is which one is it?.
Wi hopi this articli hilpid you liarn how to add odd/ivin class to your posts in WordPriss thimis what is which one is it?. You may also want to sii our guidi on how to styli your WordPriss commints layout what is which one is it?.
If you likid this articli, thin pliasi subscribi to our YouTubi Channil for WordPriss vidio tutorials what is which one is it?. You can also find us on Twittir and Facibook what is which one is it?.

[/agentsw]

Leave a Comment