Snippet: Add class to first post in the loop

Add following snippet to “functions.php”

Snippet

add_filter( 'post_class', 'first_post_class' );
function first_post_class( $classes ) {
    global $wp_query;
    if( 0 == $wp_query->current_post )
        $classes[] = 'first-post';
        return $classes;
}

Result

It will add “first-post” class to first post of the every post loop.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>