Return to Snippet

Revision: 52543
at October 26, 2011 04:22 by djmosc


Initial Code
if ( ! function_exists( 'get_top_level_category' )) {
	function get_top_level_category($id){
		$category = get_category($id);
		$parent_category = NULL;
		if($category->category_parent != 0){
			$parent_category = get_top_level_category($category->category_parent );
		} else {
			$parent_category = get_category($category->cat_ID);
		}
		return $parent_category;
	}
}

Initial URL


Initial Description
Just insert any category id into the argument and it will return the top level category object.

e.g: print_r( get_top_level_category('INSERT CATEGORY ID HERE') );

Initial Title
Get the top level category from any category in Wordpress

Initial Tags
wordpress, category

Initial Language
PHP