首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止子类别显示在自定义分类法中?

如何防止子类别显示在自定义分类法中?
EN

Stack Overflow用户
提问于 2017-06-29 13:07:18
回答 1查看 189关注 0票数 1

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

代码语言:javascript
复制
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继续显示所有的子级。我不知道怎么回事

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-29 13:35:08

如果这正是实时代码,那么您可能(我说可能,因为它没有测试此代码)在您的代码中有一个错误

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

应该是

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

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

另见参数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44826043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档