
How to Add Odd/Even Class to Posts Listing or Comments
- June 23, 2016
- Leave a comment
Your design may include different styling for odd/even comments or post lists while implementing a WordPress Theme. If a large number of comments on a post with excerpt are being shown on a blog detail page, this means styling is required here.
To accomplish this, you need to add an odd/even class to posts or comments rows. post_class and comment_class are the filters that need to be added so that these classes can be modified, respectively.
Add the following code in your Theme’s functions.php file. This will change the class for comment. The rest have to be done using CSS as there is a need to define black and grey classes here that follows the hierarchy of HTML template of comments.
1 2 3 4 5 6 7 8 9 10 11 |
<?php function greyblack_comment_class ( $classes ) { global $current_class; $classes[] = $current_class; $current_class = ($current_class == 'black') ? 'grey' : 'black'; return $classes; } add_filter ( 'comment_class' , 'greyblack_comment_class' ); global $current_class; $current_class = 'black'; ?> |
User Comments