Home > WordPress > Getting the top-level parent

Getting the top-level parent

December 25th, 2008 Leave a comment Go to comments

After hearing a lot of rave reviews, I decided to use WordPress as the blogging system for The Y-Factor. I’ve spent some time over the past couple of weeks tweaking WordPress to get it to do exactly what I want it to. It’s somewhat intimidating to get started with, but taking it a step at a time, I got to where I want to be.

One of the challenges I faced was finding the root category (the top-most category in the category tree) for any given post. Specifically, I wanted this so I can determine which “section” of The Y-Factor the reader is in – work, life, or photo blog. Based on that, I can select certain elements in the top navigation, as well as customize the links in the right navigation. One would think that there would be a simple function that already exists in WordPress to go this, but that’s not the case, so I wrote one. Keep in mind that given that WordPress only allows a single parent for each category, this shouldn’t be too hard – it simply involves traversing up the category tree until to find a category that has no parent. I was surprised to see this didn’t exist and figured that this is probably useful to others as well since I saw several people asking for it, but no solution. So, here’s my code:

function get_root_category($category_id=”)
{
 if (is_category($category_id))
 {
  $parent_cats = get_category_parents($category_id);
  $split_arr = split(“/”, $parent_cats);
  return get_cat_id($split_arr[0]);
 }
 else
  return 0;
}

Paste the above code into the functions.php file. You can either paste it in the functions.php file in the wp-includes directory, or in the wp-content/themes/<your_theme_name> directory. I put it in the former since I didn’t want the function disappearing whenever the theme was changed. Once you’ve got that in place, you simply call the function, giving it the category ID for the category whose root parent you want to find. The function will either return the root parent, or 0 if it can’t find one. If you give this function the category ID of a root category, it’ll simply return that ID to you.

Hopefully this helps some other WordPress users out there. If you have any questions, please leave comments and I’ll get back to you…

Categories: WordPress Tags:
  1. yash
    February 24th, 2009 at 11:29 | #1

    I upgraded to WP 2.7.1 recently and discovered something unfortunate – that it wiped out my functions.php file and any other WP file I had customized. This resulted in my blog being broken for several days, until I happened to stumble upon the issue earlier today.

    It’s my own fault really, I should have backed everything up before upgrading, but I didn’t. A lesson learned the hard way. So I don’t have the old files to revert to and will have to rewrite things. What does this mean for you? Well, carefully consider my suggestion about where to add the above function – though adding it to the WP functions.php file makes it available to all themes, it has the downside of being over-written when upgrading. So, either MAKE SURE YOU BACK UP, before upgrading, or just make the changes to the functions.php in your theme so that it’s not lost.

  2. February 26th, 2009 at 06:16 | #2

    Great, thanks!

  3. July 12th, 2010 at 04:28 | #3

    Thanks a lot. Been looking for this!

    Tweaked it a little so it will get the root parent of the current category, cause I called it from inside the loop. Thought i’d share it here, for who-ever likes :)

    function get_root_category()
    {
    $category = get_the_category();
    $cat_id = $category[0]->cat_ID;

    $parent_cats = get_category_parents($cat_id);
    $split_arr = split(“/”, $parent_cats);
    return get_cat_id($split_arr[0]);
    }

  4. Robert
    January 24th, 2011 at 04:53 | #4

    Please note: This method does NOT work for categories where sub-categories have smaller IDs than their parent categories, because get_category_parents() sorts the categories by their ID – so the first might not be the top category!

    http://codex.wordpress.org/Function_Reference/get_category_parents

  5. Shafshak
    April 9th, 2013 at 04:05 | #5

    I dont know if that applies in woocommerce but I want to get the top level category parent
    so i give it a different menu
    that what i wrote so far but it dont apply except on ‘wedgwood’ category
    wegdwood is my top level category

    in header.php
    if (is_product_category(‘wedgwood’))
    {
    wp_nav_menu(array(‘menu’=>’wedgwood-menu’ ));
    }

    in functions.php
    /register the custom menus
    function register_my_menus() {
    register_nav_menus(
    array(
    ‘wedgwood-menu’ => __( ‘Wedgwood Menu’ )
    )
    );
    }
    add_action( ‘init’, ‘register_my_menus’ );

    So please i need a solution for this

  6. David
    September 11th, 2013 at 13:12 | #6

    Does anyone know how we could do this for woocommerce ? I want to show the top level parent category title on the sub category pages.

  1. November 15th, 2011 at 16:51 | #1