How to fetch the highest category in the hierachry for a specific post?

Question

I understand WordPress concept “primary category”, but I do not understand how to fetch the name of the highest category in the hierarchy for a specific post?

Having this code:

$category_obj = get_the_category( $post_id );
$category_name = $category_obj[0]->cat_name;                   

would fetch the category’s name of the post, but this category is only the first category listed for the post and that category is not always the primary category.

I’m using this outside of “the loop”.

0
bestprogrammerintheworld 6 months 0 Answers 65 views 0

Leave an answer