I am trying to add a class to the first post in a custom WP_Query.
I can add a class to the first post of the standard WordPress loop using;
add_filter( 'post_class', 'featured_classes' );
function featured_classes( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = 'first';
return $classes;
}
However, when I change $wp_query
to $featured_posts
(The name of custom query) the class first
is applied to all posts. I can't work out why this might be happening.
Below is my code in full;
//Add featured post grid
add_action( 'genesis_after_header', 'post_grid' );
function post_grid() {
// Featured post Loop
$args = array (
'post_type' => 'blog',
'category_name' => 'Featured',
);
$featured_posts = new WP_Query( $args );
if ( $featured_posts->have_posts() ) {
while ( $featured_posts->have_posts() ) {
$featured_posts->the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> </div>
<?php }
} else {
echo "Sorry, no featured posts found";
}
wp_reset_postdata();
}
//Add Post Class Filter
add_filter( 'post_class', 'featured_classes' );
function featured_classes( $classes ) {
global $featured_posts;
if( 0 == $featured_posts->current_post )
$classes[] = 'first';
return $classes;
}
genesis();
Can anyone point me in the right direction?