Get active category of post in multiple categories
-
Some of my posts are added to multiple categories. For example, I have a parent category “Colours”, with sub-categories “Red”, “Blue” and “Green”. One of my posts, “Purple”, is added to both “Red” and “Blue” categories.
So the hierarchy would be as follows:
– Colours
—– Red
———– Purple
—– Blue
———– Purple
—– GreenThe “Purple” post is therefore accessible via two permalinks, ie:
1. https://www.example.com/colours/red/purple
2. https://www.example.com/colours/blue/purpleThat all works fine. The trouble I’m experiencing is getting the active sub-category ID when viewing the Purple page (single.php template) from within either of its multiple categories.
I need to know, when viewing https://www.example.com/colours/red/purple, that the sub-category is “Red”. get_the_category() is useless in this instance because it returns category ID’s for both “Red” and “Blue”.
To be more specific, I’m trying to build a dynamic breadcrumb (mirrored as a page title) that contains the full hierarchy (as illustrated by the permalink).
AFAIK, this question has come up a number of times but remains unanswered. Any help would be appreciated, thanks.
- The topic ‘Get active category of post in multiple categories’ is closed to new replies.