Showing subcategories on WordPress category pages – Yoast – Tweaking Websites


Showing subcategories on WordPress category pages

via Showing subcategories on WordPress category pages – Yoast – Tweaking Websites.

To show the subcategories for a given category on it’s category page in the sidebar, but somehow the code for this wasn’t readily available. Basically you need to check whether there are any children, and if there are, list the categories with the current category as a parent or grandparent. The following code does just that:

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

Have fun with it!   Thanks to Yoast

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

,

  1. No comments yet.
(will not be published)


5 − one =