Wordpress - 如何获取父类别 ID [英] Wordpress - How To Get Parent Category ID

查看:31
本文介绍了Wordpress - 如何获取父类别 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wordpress - 如何获取父类别 ID

Wordpress - How To Get Parent Category ID

<小时>

my category is  
news
---->sport news

我在体育新闻中有一篇文章.

i have a post in sport news.

当我进入体育新闻帖子时如何获得(新闻)ID?

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

此代码回显父猫名称

      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; 

此代码回显单页猫名

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

这个chode echo id of cat name

this chode echo id of cat name

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

我需要回显父 ID (cat_ID)请帮帮我

i need echo parent id (cat_ID) plz help me

谢谢.

推荐答案

简单,非常简单.

//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;

研究get_category

这篇关于Wordpress - 如何获取父类别 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆