How to Add Class to First Post in the WordPress Loop

In this post, I am going to share a tiny piece of code which will allow you to add a CSS class to the first post inside the WordPress loop.

Useful Read : Change Author Base URL in WordPress Permalinks

Copy the following code and paste it inside your functions.php file.

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

The above code will add a class .first to the first i.e latest post inside the WordPress Loop.

0 Comments

Leave a Comment.