How to display specific subcategories with WordPress

It used to be that after each post there were two long strings of meta data. One for categories and another for tags.

The tags have always been limited to ingredients, but the categories have slowly been increasing over time. It finally got to the point that I needed to separate them out before it gets truly chaotic. Ideally, each post needed to have a separate row for the primary, parent categories, followed by whichever subcategory options were selected for that row. For example:

Department: Shirts
Price: $20-30
Location: Second floor

Instead of how it originally displayed as:

$20-30, Second floor, Shirts

My Google searches took much longer than I’d care to admit but I finally found the most efficient way to do it. Simply open you WordPress theme editor and replace the following code:

<!--?php the_category(', '); ?-->

With this instead:

<!--?php
foreach((get_the_category()) as $childcat) {
if (cat_is_ancestor_of(15, $childcat)) {
echo '<a href="'.get_category_link($childcat-&gt;cat_ID).'"-->';
echo $childcat-&gt;cat_name . '';
}}
?&gt;

That’s it. Just make certain to replace¬†number 15 with your parent category‚Äôs ID. I ended up doing it 3 separate times (with line breaks between) for each parent category, all nestled between <ul> elements.

I hope this helps