Adding Specific Class to First Post of a Post Loop

x theme

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.

<?php
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.

Related posts:

About the author

admin

Leave a Reply

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