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'

    )
)
4

1 回答 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;
于 2013-08-16T07:57:13.103 回答