How to Add Odd Even Class to WordPress Posts

Hello friends, welcome to my another blog how to Add Odd And Even Class to WordPress Posts. If you want to design your WordPress blog posts with odd even style but you did not know the exact solutions then paste the below code in your theme functions.php.



function custom_oddeven_post_class ( $classes ) {
   global $current_class;
   $classes[] = $current_class;
   $current_class = ($current_class == 'odd') ? 'even' : 'odd'; //conditional statement
   return $classes;
}
add_filter ( 'post_class' , 'custom_oddeven_post_class' );
global $current_class;
$current_class = 'odd'; //odd class will be set in first blog

Here we use post_class filter to get the solution. Now go to your blog listings page and you will see that odd and even class added automatically into your blog. This code is tested in WordPress twentyfourteen theme. If you face any problems please comment below.

Spread the love
  •  
  •  
  •  
  • 0
  •  
  •  
  •  
  •  
  •  
  •