Exclude parent categories from the_category() within the loop
-
16-10-2019 - |
Question
I have typical parent and child categories setup as follows:
- Food
- potatoes
- corn
- beats
- Sports
- soccer
- football
- hockey
etc.
In my index.php template I'd like to list the categories of the specific post. The problem is, when I use the_category() it lists the parent categories twice. I'm using the following code:
<php echo '<dt>', the_category(', ', 'multiple'), '</dt>', "\n"; ?>
And it prints out:
Food:corn, Food, Food:potatoes
I believe it is listing the child category "corn" as "Food: corn" followed by the parent category "Food" just as "Food." Is there a way to exclude the parent categories?
The way I'd like it to read is:
Food:corn, Food:potatoes
Thank you.
Solution
Have you tagged the post in Food, Food:Corn, and Food:potatoes? Try categorizing it in Food:corn and Food:potatoes only.
I do not believe that there is a means to exclude categories from the_category() function. You will have to use another function to create a custom query to do so.