我试图在自定义WP\\U查询中向第一篇帖子添加一个类。
我可以使用将类添加到标准WordPress循环的第一篇帖子中;
add_filter( \'post_class\', \'featured_classes\' );
function featured_classes( $classes ) {
global $wp_query;
if( 0 == $wp_query->current_post )
$classes[] = \'first\';
return $classes;
}
然而,当我改变
$wp_query
到
$featured_posts
(自定义查询的名称)类
first
适用于所有职位。我不明白为什么会发生这种事。
以下是我的完整代码;
//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();
有人能给我指出正确的方向吗?