如何从get_categories对象中获取ID

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

我看了看谷歌和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 );

但是我想像上面的代码一样干净。

任何人有任何想法为什么这不起作用?


#1


我相信” cat_ID”是” term_id”的化名, 只能与一组有限的功能一起使用。你提到了get_cat_ID, 其来源显示它返回term_id。

如果将cat_id更改为cat_ID, 则你现有的代码可能会起作用-但我会改用term_id(对于”相似”的get_the_category函数, 它当然对我有用)

来源:

https://www.srcmini02.com/65358.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?