php 获取活动页面的当前类别 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8829632/
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
Get current category ID of the active page
提问by RonnieT
Looking to pull the category ID of a specific page in WordPress that is listing all posts using that specific category. Tried the below but not working. I am able to get the category name using single_term_title
.
希望在 WordPress 中提取特定页面的类别 ID,该页面列出了使用该特定类别的所有帖子。尝试了以下但不起作用。我可以使用single_term_title
.
$category = single_term_title("", false);
$catid = get_cat_ID( $category );
$category
is displaying "Entertainment" for example. But I also need the ID of "Entertainment". How would I go about this?
$category
例如显示“娱乐”。但我也需要“娱乐”的ID。我该怎么办?
采纳答案by ash108
You can try using get_the_category()
:
您可以尝试使用get_the_category()
:
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
回答by Ram Mehar Deswal
If it is a category page,you can get id of current category by:
如果是分类页面,可以通过以下方式获取当前分类的id:
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
If you want to get category id of any particular category on any page, try using :
如果您想获取任何页面上任何特定类别的类别 ID,请尝试使用:
$category_id = get_cat_ID('Category Name');
回答by Fred K
The oldest but fastest way you can use is:
您可以使用的最古老但最快的方法是:
$cat_id = get_query_var('cat');
回答by Shaffe
I use the get_queried_object function to get the current category on a category.php template page.
我使用 get_queried_object 函数在 category.php 模板页面上获取当前类别。
$current_category = get_queried_object();
Jordan Eldredge is right, get_the_category is not suitable here.
Jordan Eldredge 是对的,get_the_category 不适合这里。
回答by Jorge Orpinel
I think some of the above may work but using the get_the_category function seems tricky and may give unexpected results.
我认为上面的一些方法可能有效,但使用 get_the_category 函数似乎很棘手,可能会产生意想不到的结果。
I think the most direct and simple way to access the cat ID in a category page is:
我认为在分类页面中访问 cat ID 最直接、最简单的方法是:
$wp_query->query_vars['cat']
Cheers
干杯
回答by Bheru Lal Lohar
Alternative -
选择 -
$catID = the_category_ID($echo=false);
EDIT: Above function is deprecated please use get_the_category()
编辑:以上功能已弃用,请使用 get_the_category()
回答by guido _nhcol.com.br_
I used this for breadcrums in the category template page:
我在类别模板页面中将其用于面包屑:
$cat_obj = $wp_query->get_queried_object();
$thiscat_id = $cat_obj->term_id;
$thiscat = get_category($thiscat_id);
$parentcat = get_category($thiscat->parent);
回答by Bill
I found this question whilst looking for exactly what you asked. Unfortunately you have accepted an incorrect answer. For the sake of other people who are trying to achieve what we were trying to achieve, I thought I'd post the correct answer.
我在寻找您所问的内容时发现了这个问题。不幸的是,您接受了一个不正确的答案。为了其他试图实现我们想要实现的目标的人,我想我会发布正确的答案。
$cur_cat = get_cat_ID( single_cat_title("",false) );
As you said single_term_title("", false);
was correctly returning the category title, I'm not sure why you would have had troubles with your code; but the above code works flawlessly for me.
正如您所说的single_term_title("", false);
正确返回类别标题,我不确定为什么您的代码会遇到问题;但上面的代码对我来说完美无缺。
回答by Abhijit Patel
$cats = wp_get_post_terms( $post->ID, 'product_cat' );
foreach($cats as $cat){
/*check for category having parent or not except category id=1 which is wordpress default category (Uncategorized)*/
if($cat->parent != '0' && $cat->term_id != 1){
echo '<h2 class="link"><a href="'.get_category_link($cat->term_id ).'">'.$cat->name.'</a></h2>';
break;
}
}
回答by Robert Sinclair
Tried above for solutions to find cat ID of a post, but nothing worked, used the following instead:
在上面尝试了查找帖子的 cat ID 的解决方案,但没有任何效果,而是使用以下内容:
$obj = get_queried_object();
$c_id = wp_get_post_categories($obj->ID);