Hi i want to create a tree of categories in wordpress like this :
<ul>
<a href=""> PARENT1 </a>
<li><a href=""> CHILD 1-1</a></li>
<li><a href=""> CHILD 1-2</a></li>
.
.
.
</ul>
<ul>
<a href=""> PARENT2 </a>
<li><a href=""> CHILD 2-1</a></li>
<li><a href=""> CHILD 2-2</a></li>
.
.
.
</ul>
i want something that creates categories list in above format and show only categories which have children and hide those who don’t
I Tried Something like this but it didn’t give me what i wanted
<?php $args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false
);
$cats = get_categories( $args );
foreach( $cats as $cat) {
if($cat->parent == 0) {
$head = $cat->name;
$cat_id = $cat->term_id;
}
echo "<a href=''>" . $head . "</a>";
wp_list_cats("sort_column=NAME&optioncount=0&hierarchical=1&hide_empty=0&child_of={$cat_id}");
}
?>
(works for any taxonomies,including “category”)
Then you can output:
1) target category(taxonomy) tree, using specific ID
2) All categories/taxonomies
Thanks For Your Response , I Found the Solution :
This Code Works Fine
It’s a very old post, but maybe some more clean code. For all the options of get_terms please see the docs. Feel free to make it more generic:
And then in your template: