检索帖子类别。
原型
get_the_category( int $id = false )
描述
通过传递post id作为参数,可以在The Loop外部使用此标记。
参数
$id
(int)
(Optional)
默认为当前帖子ID。帖子ID。
返回值
(array)
数组
源文件
路径:wp-includes/category-template.php
<?php
...
function get_the_category( $id = false ) {
$categories = get_the_terms( $id, 'category' );
if ( ! $categories || is_wp_error( $categories ) )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
/**
* Filters the array of categories to return for a post.
*
* @since 3.1.0
* @since 4.4.0 Added `$id` parameter.
*
* @param array $categories An array of categories to return for the post.
* @param int $id ID of the post.
*/
return apply_filters( 'get_the_categories', $categories, $id );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_the_category/