Wordpress:如何防止子类别显示在自定义分类法中?
Wordpress: How to prevent sub-categories from showing in custom taxonomies?
我创建了自定义分类法、自定义 post 类型和自定义页面。
问题:Wordpress 显示属于该类别及其所有子类别的所有 post。这是自定义页面的代码:
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 一直显示所有子项。不知道怎么回事
如果这正是当前的代码,那么您的代码中可能(我说可能,因为它没有测试这个)有错别字
$wp_query->set('tax-query', $tax_query);
应该是
$wp_query->set('tax_query', $tax_query);
notice the underscore instead of a hyphen.
我创建了自定义分类法、自定义 post 类型和自定义页面。 问题:Wordpress 显示属于该类别及其所有子类别的所有 post。这是自定义页面的代码:
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 一直显示所有子项。不知道怎么回事
如果这正是当前的代码,那么您的代码中可能(我说可能,因为它没有测试这个)有错别字
$wp_query->set('tax-query', $tax_query);
应该是
$wp_query->set('tax_query', $tax_query);
notice the underscore instead of a hyphen.