函数原型
get_the_category( int $post_id = false ): WP_Term[]
函数描述
Retrieves post categories.
是否弃用
未弃用
函数参数
-
$post_id
int
Optional - The post ID. Defaults to current post ID.
Default:
false
函数返回值
WP_Term[] Array of WP_Term objects, one for each category assigned to the post.
函数位置
File: wp-includes/category-template.php.
函数源码
function get_the_category( $post_id = false ) {
$categories = get_the_terms( $post_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 the `$post_id` parameter.
*
* @param WP_Term[] $categories An array of categories to return for the post.
* @param int|false $post_id The post ID.
*/
return apply_filters( 'get_the_categories', $categories, $post_id );
}
源码链接
变更日志
| Version | Description |
|---|---|
| 0.71 | Introduced. |

