19

我正在尝试获取我正在查看的类别页面的当前 ID。

我检查了the_category_ID

但是当我使用时,这与我的结果相呼应

<?php $catID = the_category_ID(); ?>

有没有办法让它将值返回给变量以便隐藏?

4

8 回答 8

46

$cat当您在类别页面中时,当前类别 ID 位于全局变量中。

您可以使用以下方法对其进行测试:

<?php echo "Current Category ID is: " . $cat ;?>

例如,当您在此页面时http://example.com/category/test

于 2013-03-05T17:40:25.543 回答
8

尝试以下

$catID = get_query_var( 'cat' );

于 2013-03-05T16:35:59.487 回答
7
$category= get_queried_object();
echo $category->term_id;
于 2015-08-07T11:35:06.790 回答
5

the_category_ID已于 2003年弃用

试试这个:

if (is_category()) {
    $category = get_category(get_query_var('cat'));
    $cat_id = $category->cat_ID;
}
于 2014-04-02T15:40:23.067 回答
3

功能the_category_ID已弃用。您需要改用get_the_category()函数。例如:

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

在 wordpress codex 上查看更多信息:get_the_category

于 2014-06-12T17:33:40.237 回答
3

此代码当前用于获取类别 ID:

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

今天是 2016 年 10 月 18 日,这对我有用。

于 2016-10-14T15:30:06.223 回答
0

这会写入变量而不是回显

<?php $catID = the_category_ID($echo=false);?>
于 2013-03-05T16:48:44.190 回答
-3

您将在变量中获得当前类别 ID,

<?php $catID = the_category_ID($echo);?>

这不直接打印,只要给打印语句那个时候就打印。

于 2015-09-16T13:17:54.580 回答