My category structure is as follows:

- Top Category
---- Sub Category 1
------- Sub Sub Category 1.1
------- Sub Sub Category 1.2
------- Sub Sub Category 1.3
---- Sub Category 2
------- Sub Sub Category 2.1
------- Sub Sub Category 2.2
------- Sub Sub Category 2.3

I'm on a post under 1.2 so it would be:

Top Category -> Sub Category 1 -> Sub Sub Category 1.2 -> Current Post

NB: In the post ONLY "Sub Category 1" and "Sub Sub Category 1.2" are selected as categories ("Top Category" is not checked).

Now, how do I get get the slug of the Top Category ("top-category"), navigating backward?



get_ancestors() returns an array containing the parents of any given object.

This example has two categories. The parent with the id of 447 and the child with a id of 448 and returns the a category hierarchy (with IDs):

get_ancestors( 448, 'category' ); 


    [0] => 447

get_ancestors Codex Page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.