• Hello,

    I am using this code:

    <?php $category = get_the_category(); echo $category[0]->category_nicename;?>

    Which works fine, but it gets me the lowest category associated with the post. How would I change it to get me the top most parent category?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Would cat_is_ancestor_of help? My guess is that you’d need to grab all the categories and then run a quick loop/function to find out which is the highest in the category tree.

    Thread Starter dlogan

    (@dlogan)

    From what the article says, it won’t work. It just returns a true or false.

    True or false is all you need to create a code snippet to do this. The other thing you could do is check each category’s parent using $category->category_parent.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get just the top most parent category?’ is closed to new replies.