Well, you can use get_category_parents() (see link to read arguments, etc.). This will return a list of all parents in a hierarchical order. In this case, if you just need the most immediate parent category, you can do something like this:
<?php $parent_cats = get_category_parents( $cat, false, ',' ); $parent_cat = explode(",", $parent_cats); echo $parent_cat[0]; ?>
This will reflect the first parent category (the one immediately above your current category).
To be clear:
in get_category_parents()
- arg 1 - category id (I just used $cat arbitrarily)
- arg 2 - this is if you want wp to add a link to each returned parent category
- arg 3 - separator used to separate returned categories, if any
in explode()
- arg 1 is the separator to look for to split the string in the array
- arg 2 is the source string to split
Happy coding!
source share