Wordpress获取当前类别ID

ngp*_*und 15 wordpress

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

我检查了the_category_ID

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

<?php $catID = the_category_ID(); ?>
Run Code Online (Sandbox Code Playgroud)

有没有办法让它将值返回给变量,使其隐藏?

bir*_*ire 43

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

你可以测试它:

<?php echo "Current Category ID is: " . $cat ;?>
Run Code Online (Sandbox Code Playgroud)

当你在这个例子这个页面 http://example.com/category/test


Jro*_*rod 8

请尝试以下方法

$catID = get_query_var( 'cat' );


小智 7

$category= get_queried_object();
echo $category->term_id;
Run Code Online (Sandbox Code Playgroud)

  • 这是实际有效的最佳答案。不错的工作。 (2认同)

Tim*_*Tim 5

the_category_ID在2003年弃用.

试试这个:

if (is_category()) {
    $category = get_category(get_query_var('cat'));
    $cat_id = $category->cat_ID;
}
Run Code Online (Sandbox Code Playgroud)


ngp*_*und 0

这会写入变量而不是回显

<?php $catID = the_category_ID($echo=false);?>
Run Code Online (Sandbox Code Playgroud)

  • 函数已弃用。 (5认同)