【问题标题】:Wordpress: How to prevent sub-categories from showing in custom taxonomies?Wordpress:如何防止子类别显示在自定义分类法中?
【发布时间】:2017-06-29 13:07:18
【问题描述】:

我创建了一个自定义分类法、一个自定义帖子类型和一个自定义页面。 问题: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 一直显示所有子项。不知道怎么回事

【问题讨论】:

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


    【解决方案1】:

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

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

    应该是

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

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

    另见WP_Query#Taxonomy_Parameters

    【讨论】:

    • 非常感谢。有用!但是,此外。我必须在: /* if ($active_term->parent == 0) 和 else { */ $tax_query = array(
    • 非常感谢。有用!但是,此外。我必须在以下之间发表评论: /* if ($active_term->parent == 0) 和 else { */ $tax_query = array( 以及 $wp_query->set('tax-query', $tax_query); 的前一行// } 为重复评论道歉,但我无法在 5 分钟后编辑相同的评论。
    猜你喜欢
    • 2012-03-19
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多