Home » WordPress Tutorials » You are reading »

How to Show All Child Categories from Parent Category in WordPress

child-cat-in-parent-cat
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 
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;
} 
?>

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.

Related Topics:

WordPress Tutorials
Building Your Website with WordPress? Learn How to Optimize it
WordPress Tutorials
How to Detect Mobile or Tablet Condition in WordPress
WordPress Tutorials
Designing and Protecting Your WordPress Site is Easier Than You Think
WordPress Tutorials
The Ultimate SEO Tips For Your WordPress Blog
How to Show All Child Categories from Parent Category in WordPresshttp://cdn.dezzain.com/1/2013/10/child-cat-in-parent-cat.pnghttp://cdn.dezzain.com/1/2013/10/child-cat-in-parent-cat-150x150.pngRichie KS WordPress Tutorialswordpress coding,WordPress TipsLearn how to show all child categories from parent category in WordPress. When you browsing your parent category, all the sub categories of the parent category will automatically showed. DEZZAIN.COM
Enjoy This Article?, Follow us or Share it smiley
viral and buzz WordPress theme

11 Comments Already

  1. I was able to get this to work, thanks! I was wondering if there is a way to have a link back to the parent category from the child as well?

    Public CommentUserI was able to get this to work, thanks! I was wondering if there is a way to have a link back to the parent category from the child as well?
  2. I don’t know Where Do I Put This Code?
    Thanks

    Public CommentUserI don't know Where Do I Put This Code? Thanks
    • usually in archive.php

      Member of DEZZAIN.COMAdministratorusually in archive.php
  3. Thank you very much. You save my time. I used your snippet code on my site.

    Public CommentUserThank you very much. You save my time. I used your snippet code on my site.
  4. First off, excellent theme and thank you so much for the ongoing support.

    I have implemented the script above which is working for me. Is there an additional script or is it a CSS change so that I can see the child categories upon hover while on the homepage?
    Regards,
    Matthew

    Public CommentUserFirst off, excellent theme and thank you so much for the ongoing support. I have implemented the script above which is working for me. Is there an additional script or is it a CSS change so that I can see the child categories upon hover while on the homepage? Regards, Matthew
1 2

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


You can wrap php, js or style css code with [PHP]..your code...[/PHP]