Displaying Subcategories in WordPress

This is a great code snippet for displaying subcategories in WordPress.I use the following code snippet on my category pages to display subcategories (if any).

 if (is_category()) { $this_category = get_category($cat); if (get_category_children($this_category->cat_ID) != "") { echo "<ul>"; wp_list_categories('orderby=id&hide_empty=0&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID); echo "</ul>"; } }

So, let me explain this code snippet a bit. Firstly, the code snippet will ask if this is a category page, if it is, it goes on to check if the category has any subcategories. If it does, it goes on further to display the subcategories in a unordered HTML list.

The code snippet makes use of the wplistcategories template tag. You can read more about it here and how to expand it using simple arguments.

Please note: I have added the hide_empty argument, which will display subcategories even if there are no posts. Change the value to 1 (true) or remove the argument, if you want to hide subcategories with no posts.