Wordpress the_post方法在自定义类别页面不显示顶部浏览的帖子


wordpress the_post method in custom category page not showing top viewed post

我有查看最多的帖子(在客户类别中),在主页中我添加了这个小部件,它工作完美,在详细页面中它也工作完美,但在类别页面中相同的小部件行为奇怪

$r = new WP_Query( array( 'tax_query' => 
                    array(
                        'relation' => 'OR',
                        array(
                            'taxonomy' => 'custcategory', 
                            'field' => 'term_id', 
                            'terms' => array(10)), 
                        ),
                        'category__in'=>array(10),
                        'post_type'=> $post_type , 
                        'posts_per_page' => $number, 
                        'meta_key' => 'post_views_count', 
                        'orderby' => 'meta_value_num', 
                        'order' => 'DESC'  ) ); 
if ($r->have_posts()) :
    // Enters this block in home page and detail page
    while ( $r->have_posts() ) : $r->the_post();
else:
    // Enters this block in category page
有人知道为什么会有这种奇怪的行为吗?

也许你有一些过滤器正在影响类别页面,将其添加到您的查询参数:

'suppress_filters' => true

您也可以尝试在主查询之后添加wp_reset_query(),但这应该不是必需的,因为您正在声明一个新的WP_Query。

我刚刚意识到你的查询参数是错误的。您已经设置了一个关系,但是您只有一个分类法数组。此外,您不需要'category__in'=>array(10)参数,它仅用于默认分类法"category",并且您只想要自定义分类法"custcategory"中的帖子,不是吗?

你的查询应该是:

$r = new WP_Query( array(
    'tax_query' => array(
        array(
            'taxonomy' => 'custcategory', 
            'field' => 'term_id', 
            'terms' => array(10)
        ), 
    ),
    'post_type'=> $post_type , 
    'posts_per_page' => $number, 
    'meta_key' => 'post_views_count', 
    'orderby' => 'meta_value_num', 
    'order' => 'DESC' 
));