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

时间:2013-08-14 11:31:30

标签: wordpress categories taxonomy

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

这是我的代码

$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 :(得分: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;