get_cat_ID()
get_cat_ID( string $cat_name )
从类别名称中检索该类别的ID。
Retrieve the ID of a category from its name.
参数(Parameters)
参数 | 类型 | 必填 | 说明 |
---|---|---|---|
$cat_name | (string) | 必需 | 类别名称。 |
返回(Return)
(int)0,如果失败,则返回成功时的类别ID。
源码(Source)
/** * Retrieve the ID of a category from its name. * * @since 1.0.0 * * @param string $cat_name Category name. * @return int 0, if failure and ID of category on success. */ function get_cat_ID( $cat_name ) { $cat = get_term_by( 'name', $cat_name, 'category' ); if ( $cat ) return $cat->term_id; return 0; }
更新版本 | 源码位置 | 使用 | 被使用 |
---|---|---|---|
1.0.0 | wp-includes/category.php:195 | 2 | 1 function |
笔记(Notes)
例子:
对于需要类别ID的函数(如category_description())很有用:
get_cat_ID() 为WP2原创文章,链接:https://www.wp2.cn/functions/get_cat_id/