如何在父分类下显示帖子,但不在子分类下显示帖子

问题描述 投票:0回答:1

如何显示父分类下的帖子而不是子分类下的帖子。

这是我的代码

$post_type = 'products';
$tax = 'products_categories';
$newargs=array(
                  'post_type' => $post_type,
                     'tax_query' => array (
    array(
        'taxonomy' => 'products_categories',
        'field' => 'id',
        'terms' => '$term_id'

    )
)
php wordpress categories taxonomy
1个回答
0
投票

我自己找到了答案

这是新代码,仅显示父类别的帖子,而不显示子类别的帖子

if (is_tax()) {
    if (get_query_var('securityproducts_categories')) {
        $taxonomy_term_id = $wp_query->queried_object_id;
        $taxonomy = 'securityproducts_categories';
        $unwanted_children = get_term_children($taxonomy_term_id, $taxonomy);
        $unwanted_post_ids = get_objects_in_term($unwanted_children, $taxonomy);
        // merge with original query to preserve pagination, etc.
        query_posts( array_merge( array('post__not_in' => $unwanted_post_ids), $wp_query->query) );
    }
}
 while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

// Your code

endwhile;
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.