Wordpress - 如何获取父类别 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19961130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress - How To Get Parent Category ID
提问by P00ZHAN .
Wordpress - How To Get Parent Category ID
Wordpress - 如何获取父类别 ID
my category is
news
---->sport news
i have a post in sportnews.
我在体育新闻上有一个帖子。
how to get the parent(news) id when i go into the post of sport news?
当我进入体育新闻的帖子时如何获得父母(新闻)ID?
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
这个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
我需要 echo parent id (cat_ID) 请帮帮我
thanks.
谢谢。
回答by Ivan Hanák
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
学习 get_category
回答by acbaltaci
$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
回答by Saurabh Panchal
<?php
if (is_category())
{
$thiscat = get_category( get_query_var( 'cat' ) );
$catid = $thiscat->cat_ID;
$parent = $thiscat->category_parent;
if (!empty ($catid) ) {
$catlist = get_categories(
array(
'child_of' => $catid,
'orderby' => 'id',
'order' => 'ASC',
'exclude' => $parent,
'hide_empty' => '0'
) );
?>
<div class="subcat-list">
<ul>
<?php foreach ($catlist as $category) { ?>
<li><a href="<?php echo get_category_link( $category->term_id ); ?>"><?php echo $category->name; ?></a></li>
<?php } ?>
</ul>
</div>
<?php } } ?>