WordPress - 如何获取父类别 ID
my category is
news
---->sport news
我有一个帖子在sport news.
如何获得parent(新闻)我进入体育新闻帖子时的ID?
这段代码回显父猫的名字
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 猫的名字
$category = get_the_category(); echo $category[0]->cat_ID;
我需要回显父 ID (cat_ID)
请帮助我
谢谢。
简单,非常简单。
//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 http://codex.wordpress.org/Function_Reference/get_category
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)