get_the_category()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_the_category( int|false $post_id = false ): WP_Term[]
Описание
Этот тег можно использовать вне цикла, передав ID записи в качестве параметра.
Примечание: функция возвращает результаты только из стандартной таксономии «category».
Для произвольных таксономий используйте get_the_terms() .
Оригинал (английский)
This tag may be used outside The Loop by passing a post ID as the parameter.
Note: This function only returns results from the default “category” taxonomy.
For custom taxonomies use get_the_terms() .
Параметры
$post_id
int|false
необязательный
= false
Возвращаемое значение
WP_Term[]
Исходный код
wp-includes/category-template.php:77
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 );
}
История изменений
| Версия | Описание |
|---|---|
| 0.71 | Introduced. |

