Posted By

djmosc on 10/26/11


Tagged

wordpress category top level Parent


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

DaR3Y


Get the top level category from any category in Wordpress


 / Published in: PHP
 

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

e.g: printr( gettoplevelcategory('INSERT CATEGORY ID HERE') );

  1. if ( ! function_exists( 'get_top_level_category' )) {
  2. function get_top_level_category($id){
  3. $category = get_category($id);
  4. $parent_category = NULL;
  5. if($category->category_parent != 0){
  6. $parent_category = get_top_level_category($category->category_parent );
  7. } else {
  8. $parent_category = get_category($category->cat_ID);
  9. }
  10. return $parent_category;
  11. }
  12. }

Report this snippet  

You need to login to post a comment.