如何从 get_categories 对象中获取 id

How to get the id out of the get_categories objects

我想使用 get_categories 函数显示我的类别的 ID。

我查看了 google 和 stackexchange,发现了这两种方法

$categories = get_categories( 
    array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) 
);

foreach( $categories as $category ) {
    echo '<p>' .'Category id'. $category->cat_id . '</p>';
    // the method above and below are the ones i found
    // but both the id and cat_id give no results
    echo '<p>' .'Category id'. $category->id . '</p>';

}

我知道我可以使用:

echo get_cat_ID( $category->name );

但我希望它像上面的代码一样干净。

有人知道为什么这不起作用吗?

我认为 "cat_ID" 是 "term_id" 的化名,只能用于有限的功能集。你提到 get_cat_ID 及其 source shows it returns term_id.

如果您将 cat_id 更改为 cat_ID[,您现有的代码可能会起作用=22=] - 但我会使用 term_id 代替(它对我来说肯定适用于 "similar" get_the_category函数)