Display Categories and Subcategories In Hierarchical Order List In WordPress is easy with this article guide. Sometime when using WordPress custom template, you do not want to add the child category from parent category tree structure one by one. However you want to display child category when you are browsing parent category, this is useful if you have assign number of sub-category for a certain category.

How To Display Categories and Subcategories In Hierarchical Order List In WordPress

There is really no easy way to do this and some ways that are out there on the web has various bugs. If you want to show a category and subcategory “tree like” list on a WordPress page. In this article, we will show you how you can display categories and subcategories in hierarchical order list in WordPress template.

Why Display Categories and Subcategories In Hierarchical Order List?

Usually, you will be display only top level parent category into your custom menu or sidebar menu but all the others child and grandchild category will be hidden from the site so users will click on parent then show child.

<?php global $wpdb; $args = array( 'type'                     => 'post',
'child_of'                 => 0,
'parent'                   => 0,
'orderby'                  => 'name',
'order'                    => 'ASC',
'hide_empty'               => 0,        // hide empty categories [0] false
'hierarchical'             => 2,
'exclude'                  => 1,
'include'                  => '',
'number'                   => '',
'taxonomy'                 => 'category',
'pad_counts'               => false
);
$categories = get_categories( $args );
?>


<ul class="nav" id="side-menu">

<?php foreach ($categories as $category) { $cat_link=get_category_link($category->term_id);
echo '
<li class="title"><a href="'.$cat_link.'"><i class="fa fa-tags fa-1"></i>   '.$category->cat_name.'</a>';
$theid = $category->term_id;

$children = $wpdb->get_results("SELECT term_id FROM wp_term_taxonomy WHERE parent='".$theid."'");

$no_children = count($children);
if ($no_children > 0) {
echo "<!-- span class='fa arrow'></span -->
<ul class='nav nav-third-level'>";
$args2 = array(
'orderby' => 'name',
'hide_empty'=> 0,
'parent' => 2
);
$args2["parent"]=$category->term_id;
$categories2 = get_categories( $args2 );
foreach ($categories2 as $category2) {
$cat_link1=get_category_link($category2->term_id);
echo '
<li><a href="'.$cat_link1.'">'.$category2->cat_name.'</a></li>

';
}
echo '</ul>

';
} else {
echo '</li>

';
}
}
?>

</ul>

In this article, its easy to show all the child list from the current parent category you are viewing. Just add this snippet code into anywhere you want in WordPress template, preferably in sidebar or some where near the navigation menus.

If no assign any post to category, you need to add ‘hide_empty’=> 0 in $args array. Otherwise your category response return empty.

Also Visit: How To Update URLs In WordPress Website After Moving to New Domain

We hope this article helped you to display categories and subcategories in hierarchical order(tree view) list On WordPress.

LEAVE A REPLY