我不确定我是如何做到这一点的,我已经在后端正确地设置了它,因此,例如,我有一个类别“酒吧和酒吧”,还有3个其他类别“x”“y”“z”,所有这些家长都是酒吧和酒吧。我想在单击父类别时实现类似的效果,然后它将显示子类别。
<div class=" cus-fw-tabs">
<ul>
<li><a href="#tabs-1">All</a></li>
<?php
$args = array(
\'orderby\' => \'ASC\',
\'order\' => \'ASC\',
\'hide_empty\' => false,
\'exclude\' => array(),
\'exclude_tree\' => array(),
\'include\' => $term_arr,
\'number\' => \'\',
\'fields\' => \'all\',
\'slug\' => \'\',
\'parent\' => \'\',
\'hierarchical\' => true,
\'child_of\' => 0,
\'childless\' => false,
\'get\' => \'\',
\'name__like\' => \'\',
\'description__like\' => \'\',
\'pad_counts\' => false,
\'offset\' => \'\',
\'search\' => \'\',
\'cache_domain\' => \'core\'
);
$terms = get_terms(\'category\',$args);
if(!empty($terms)) {
$i=2;
foreach($terms as $term){
?>
<li class="tabs-<?php echo $term->term_id; ?>">
<a class="term-heading" href="#tabs-<?php echo $term->term_id; ?>"><?php echo $term->name; ?></a>
</li>
<?php
$i++;
}
}
?>
</ul>
</div>
SO网友:S. Coll
获取父类别$category = get_category_by_slug( \'category-name\' );
设置post查询所需的任何参数,但请确保包括child_of
arg参数
$args = array(
\'type\' => \'post\',
\'child_of\' => $category->term_id,
\'hierarchical\' => 1,
\'taxonomy\' => \'category\',
);
根据这些参数获取子类别
$child_categories = get_categories($args);
您将得到一个关联数组,可以使用foreach
或者任何你想要的迭代函数。如果您想在前端应用程序方面更详细地解决特定问题,请随时更新您的问题或发表评论。