Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress - How To Get Parent Category ID

Tags:

wordpress

Wordpress - How To Get Parent Category ID


my category is  
news
---->sport news

i have a post in sport news.

how to get the parent(news) id when i go into the post of sport news?

this code echo parent cat name

      foreach((get_the_category()) as $childcat) { $parentcat = $childcat->category_parent;  
echo get_cat_name($parentcat);
echo $parentcat->term_id;}   
        echo $post->post_parent->cat_ID; 

this code echo single page cat name

   global $post;$category = get_the_category($post->ID);echo $category[0]->name;

this chode echo id of cat name

        $category = get_the_category();    echo $category[0]->cat_ID; 

i need echo parent id (cat_ID) plz help me

thanks.

like image 785
P00ZHAN . Avatar asked Nov 13 '13 18:11

P00ZHAN .


2 Answers

Simply, very simply.

//firstly, load data for your child category
$child = get_category(31);

//from your child category, grab parent ID
$parent = $child->parent;

//load object for parent category
$parent_name = get_category($parent);

//grab a category name
$parent_name = $parent_name->name;

Study get_category

like image 56
Ivan Hanák Avatar answered Oct 06 '22 03:10

Ivan Hanák


$thiscat =  get_query_var('cat'); // The id of the current category
$catobject = get_category($thiscat,false); // Get the Category object by the id of current category
$parentcat = $catobject->category_parent; // the id of the parent category 
like image 32
acbaltaci Avatar answered Oct 06 '22 03:10

acbaltaci