首页 文章

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

提问于
浏览
0

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

这是我的代码

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

    )
)

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;
    

相关问题