List Sub Categories of Parent Category

I just spent a good Friday resolving something that should be very simple. Of course it’s snowing like crazy so I’m not minding. On the Wake Magazine site I’ve been working on, we have a Blogs category that contains multiple sub categories. It’s a very newspaper-like site with multiple writers and thus multiple voices needing their own blog section. I wanted to list out all those sub categories anywhere in the blog area. I already addressed the magic (or insanity) of wp_list_categories so I won’t go there but my question was basically how do I call forth the parent category in a child category page? The solution was pieced together with some searching of the forums and uses a rare function cat_is_ancestor_of that I had no idea existed. Probably most WordPress run websites do not utilize the category terminology very deeply and so those issues are not often touched on. This solution also conditionally display only if you are on sub category page.

<?php
 foreach((get_the_category()) as $childcat) {
   $parentcat = $childcat->category_parent;
   if (cat_is_ancestor_of($parentcat, $childcat)) : ?>
     <div id="subnav">
     <h4>Insert a title or have it pulled from the parent cat</h4>
     <ul>
     <?php wp_list_categories('orderby=id&show_count=0&title_li=&child_of='.$parentcat); ?>
     </ul>
     </div>
   <?php endif; 
   } ?>

More Posts

Donate

Hey was this useful? Please consider tipping or subscribing as a member. Even just $1 or $5 can help me improve this content and keep it ad-free.