Select Page

The standard WordPress loop can be used to display posts on any page of your website. By default, each post in the loop will have the class “post” applied to it. However, you may want to add an additional class to the first post in the loop (for example, to style the first post differently from the others).

To do this, you can use WordPress hooks to add a custom function that will add the desired class to the first post in the loop.

Here is the code you can use:

add_filter('post_class', 'my_first_post_class');

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

The code above uses the “post_class” filter to add a custom function (named “wpobsessed_first_post_class”) that will add the class “first-post” to the first post in the loop. To use this code, simply add it to your theme’s functions.php file or a custom plugin file.