Sometime when using WordPress custom menu, you do not want to add the child category from parent category into the created menus. However you want the child category to show when you are browsing parent category, this is useful if you have a wide variety of sub-category for a certain category. Take for example from above image,
Art And Design (Top Level Parent Category)
– Print Design (Secondary Child Category)
– Technology (Grand Child Category)
– Web Design (Secondary Child Category)
Usually, you will be adding top level parent category into your custom menu but all the others child and grandchild category will be hidden from the site so users will not be able to browse them.
In this tutorial, its easy to show all the sub categories from the current parent category you are viewing. Just add this snippet code into anywhere you want in template, preferably in sidebar or some where near the navigation menus.
[php]<?php
if ( is_category() ) {
$this_category = get_category($cat);
if($this_category->category_parent):
else:
$this_category = wp_list_categories(‘orderby=id&depth=5&show_count=0&title_li=&use_desc_for_title=1&child_of=’.$this_category->cat_ID."&echo=0");
echo ‘<ul>’. $this_category . ‘</ul>’;
endif;
}
?>[/php]
Final Result
Now when i’m currently browse the ‘Art and Design’ category, the sub category for this category will automatically show in bottom of the navigation where i’ve place the snippet code above.
You can change the depth=5 to how many sub level you want to show within the parent category.