Get Top Parent Category (WordPress)

There are many times when you need to show or get the top most (root) parent category in WordPress - regardless of how many subcategories you might be deep. I have used this logic for page navigation (highlight the top parent tab) - as well as within some custom loops/sidebar code.

They way to do this:

PHP:
  1. <?php
  2. $parentCatList = get_category_parents($cat,false,',');
  3. $parentCatListArray = split(",",$parentCatList);
  4. $topParentName = $parentCatListArray[0];
  5. $sdacReplace = array(" " => "-", "(" => "", ")" => "");
  6. $topParent = strtolower(strtr($topParentName,$sdacReplace));
  7. ?>

To test this you can simply put it in your header and echo out $topParent and you will see the "slug" of the category.
If you want to see the category name and not necessarily the slug - you can simply echo $topParentName.

24 Responses to “Get Top Parent Category (WordPress)”

  1. Chris M says:

    It just echos out “Object”..

    • Ngqabutho says:

      @ those interested

      post;
      $category = get_the_category();
      $parent = get_cat_name($category[0]->category_parent);
      if ($parent == “Interviews” ) {
      include(TEMPLATEPATH . ‘/single_interview.php’);
      } else {
      include(TEMPLATEPATH . ‘/single_blog.php’);
      }

      ?>

      this sees if the parent is “Interviews” and opens “single_interview.php”

      this is the whole code for the “single.php”… coz the redirect is the normal single post template which it “single_blog.php”

  2. jzelazny says:

    Put a if (is_category()) {} around the code and then echo it out. Check out the full example: http://www.sandboxdev.com/downloads/wordpress-logic/top-category.phps

  3. f1shman says:

    thanks alot!!
    i was searching for a long time to find such a solution ;)

  4. f1sHMaN says:

    hmm, why isn’t it working on the front page?
    what can I change that it will output the cat. name on the frontpage?
    I want to add the top parent category name as a class!

    thx
    greets froms switzerland

  5. jzelazny says:

    Hello f1sHMaN from Chicago! Do you have this logic within (is_category()){}? Make sure you check out the comment above yours where you can see the logic in action.

  6. f1sHMaN says:

    that is the problem! what can i change to have the name of the topcategory (topParentName) on the frontpage?

    your script only goes on the category page.
    On the frontpage it returns: object…
    thx alot!

    • Mr. Manoj says:

      What u want this code is only applicable for single template and category template.

    • Ngqabutho says:

      post;
      $category = get_the_category();
      $parent = get_cat_name($category[0]->category_parent);
      if ($parent == “Interviews” ) {
      include(TEMPLATEPATH . ‘/single_interview.php’);
      } else {
      include(TEMPLATEPATH . ‘/single_blog.php’);
      }

      ?>

  7. Sarah says:

    This is great! Thank you so much!!!

  8. Sarah says:

    I have a question:
    How can I make this work for the single view of a post.

    Say I have a category Gallery and a category Work.
    With your code I have body class = category_gallery for the Gallery and body class = category_work for the work category. Great!

    Now I’d like to have body class = single_gallery when on the single view for any post in the Gallery category and body class = single_work when on a single post in the work category.

    I hope that’s not too confusing.
    How would this be done?

  9. Katherine says:

    Any way to make it a live link?? I’m sure there IS a way… I’m just not that savvy when it comes to wordpress codex. Any help would be appreciated!!

  10. Rosario says:

    Wow.. Thanks for the list :)

  11. discostrings says:

    Thanks! You just saved me some time and frustration in hacking this out myself.

  12. Alen says:

    Big thanks ! Your code resolved my need to display banners by category groups (aka parents).
    Thank you again !

  13. mark says:

    Thanks for this.Im going to give it a try.

  14. Reino says:

    This works for me! Thanks!

  15. Eddard Stark says:

    Great! I have a question dough. Ik kan know get the top parent name and the top parent slug. How do i get de top parent ID?

  16. Car insurance claims >> http://onlinecarinsuranceclaims.com/ says:

    [... - http://www.sandboxdev.com is other nice place of tips. Online Car insurance claims [… -

  17. Mike D says:

    Much thanks for this.

    Change $topParentName = $parentCatListArray[0];
    To Change $topParentName = $parentCatListArray[1];

    and to get the second category down if your in a subcategory.

  18. Antonio says:

    This is EXACTLY what i was searching for! Thanks for sharing this snippets.

Leave a Reply