admin管理员组文章数量:1414923
I'm not sure how i do this, i have set it up correctly in the back end so for example i have a category 'bars and pubs' and i have 3 other categories 'x' 'y' 'z' and all there parents are bars and pubs. I want to achieve something similar when i click on the parent category it will then show the child categories.
<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>
I'm not sure how i do this, i have set it up correctly in the back end so for example i have a category 'bars and pubs' and i have 3 other categories 'x' 'y' 'z' and all there parents are bars and pubs. I want to achieve something similar when i click on the parent category it will then show the child categories.
<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>
Share
Improve this question
asked Mar 28, 2017 at 9:10
JakeJake
312 bronze badges
1 Answer
Reset to default 1Grab the parent category $category = get_category_by_slug( 'category-name' );
Set whatever args you need for your post query, but make sure to include the child_of
arg
$args = array(
'type' => 'post',
'child_of' => $category->term_id,
'hierarchical' => 1,
'taxonomy' => 'category',
);
Get your subcategories based on these args $child_categories = get_categories($args);
You'll get an associative array back, which you can loop over using foreach
or whatever iterative function you want. If you want a more detailed solution to a specific problem in terms of a front-end application of this, feel free to update your question or post a comment.
本文标签: Get child categories when clicking on parent category
版权声明:本文标题:Get child categories when clicking on parent category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745178532a2646357.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论