Wordpress:如何防止子类别出现在自定义分类中?

时间:2017-06-29 13:07:19

标签: php wordpress custom-post-type children custom-taxonomy

我已经创建了自定义分类,自定义帖子类型和自定义页面。 问题:Wordpress显示属于该类别及其所有子类别的所有帖子。这是自定义页面的代码:

global $wp_query;
$wp_query->set('post_type', $postType);
$wp_query->set('orderby', $orderBy);
$wp_query->set('order', 'ASC');
$wp_query->set('posts_per_page', 12);
$active_term = get_term_by('slug', $term, $taxonomy);
if ($active_term->parent == 0)
{


    $terms = get_terms( array(
        'taxonomy' => $taxonomy,
        'hide_empty' => false,
        'parent' => $active_term->term_id,
        'orderby' => 'term_id',
        'order' => 'ASC'
    ) );
    $slug_array = array();
    if( count($terms) > 0 )
    {
        foreach ($terms as $t){
            $slug_array[] = $t->slug;
        }
        $the_slug = $terms[0]->slug;

        $active_title = $terms[0]->name;
    }
    else
    {
        $the_slug = $active_term->slug;
        $slug_array = array($the_slug);
        $active_title = $active_term->name;
    }
    $tax_query = array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => $slug_array,
            'include_children' => false
        )
    );

}
else
{

    $tax_query = array(
        array(
            'taxonomy' => $taxonomy,
            'field' => 'slug',
            'terms' => array ( $active_term->slug ),
            'include_children' => false
        )
    );
    $the_slug = $active_term->slug;
    $active_title = $active_term->name;
}
$wp_query->set('tax-query', $tax_query);
$wp_query->get_posts();

如您所见,选项'include_children'=>设置为false并且Wordpres会一直显示所有子项。我不知道出了什么问题

1 个答案:

答案 0 :(得分:1)

如果这正是生活的代码,那么你可能(我说可能,因为它没有测试这个)在你的代码中有一个拼写错误

@class ABC

应该是

$wp_query->set('tax-query', $tax_query);

  

注意下划线而不是连字符。

另见WP_Query#Taxonomy_Parameters