Hi I am trying to find the top most category of a post. I tried finding any WP builtin functions but failed.
For example I have categories like this.
Parent
sub-1
sub-2
And I have a post in sub-2. So with the ID of sub-2, I am trying to find the ID of top most category which is named "Parent" in this example.
Look at this helpful script: Get Top Level Parent Category Id Of a Single Post
And you can change this part:
To
To get the name of the top level category
I needed parent ID and this worked nice and simple for me:
Ok I ended up building my own function to get the top most level category.
For the ones having problems with this method I found a simple solution, probably not the best one in terms of efficiency. To get the top level category parent from post/product page
Usage:
Function:
The good answer today is:
Because get_the_category wont return "sub1" category if the post is just in "sub2", even if "sub2" is a sub category of "sub1". And you can get the cat ID with get_cat_ID...
try this