Wordpress 获取当前类别 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15228823/
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 getting current category ID
提问by ngplayground
I'm trying to get the current ID of the category page I'm viewing.
我正在尝试获取我正在查看的类别页面的当前 ID。
I had checked the_category_ID
我检查过the_category_ID
But this echo'd my result when I used
但是当我使用时,这与我的结果相呼应
<?php $catID = the_category_ID(); ?>
Is there a way to get it to return the value to the variable so its hidden?
有没有办法让它将值返回给变量以便隐藏?
采纳答案by ngplayground
This writes the variable instead of echoing
这将写入变量而不是回显
<?php $catID = the_category_ID($echo=false);?>
回答by birgire
The current category ID is in the global $cat
variable, when you are in a category page.
$cat
当您在类别页面中时,当前类别 ID 位于全局变量中。
You can test it with:
您可以使用以下方法对其进行测试:
<?php echo "Current Category ID is: " . $cat ;?>
when you are in for example this page http://example.com/category/test
例如,当您在此页面中时 http://example.com/category/test
回答by Jrod
Try the following
尝试以下
$catID = get_query_var( 'cat' );
$catID = get_query_var( 'cat' );
回答by Tim
the_category_ID
was deprecated in 2003.
Try this:
尝试这个:
if (is_category()) {
$category = get_category(get_query_var('cat'));
$cat_id = $category->cat_ID;
}
回答by tejashree mahadik
$category= get_queried_object();
echo $category->term_id;
回答by Byteshifter
Function the_category_ID
is deprecated. You need to use get_the_category()
function instead. E.g.:
功能the_category_ID
已弃用。您需要改用get_the_category()
函数。例如:
$category = get_the_category();
echo $category[0]->cat_name;
See more at wordpress codex: get_the_category
在 wordpress codex 中查看更多信息:get_the_category
回答by PUSTAKAKORAN.COM
This code current to get Category ID:
此代码当前用于获取类别 ID:
<?php
$category = get_the_category();
echo $category[0]->cat_ID;
?>
It's work for me, today 18 Oct 2016.
2016 年 10 月 18 日今天,这对我有用。
回答by Samyappa
You will get current category id in variable,
您将在变量中获得当前类别 ID,
<?php $catID = the_category_ID($echo);?>
This not print directly, whenever give print statment that time only print.
这个不直接打印,每当给打印语句的时候才打印。