Adding Specific Class to First Post of a Post Loop

x theme
(Last Updated On: July 10, 2014)

Following is a code snippet to add a specific class, for example “my_class” to the first post of a post loop. You’ll have to insert the code inside functions.php. So, “my_class” class will be added to the first post of every post loop, you’re running.

add_filter( 'post_class', 'be_first_post_class' );
function be_first_post_class( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = 'my_class';
return $classes;

Thanks to Bill Erickson for this code.

About the author


Leave a Reply

Your email address will not be published. Required fields are marked *