On this page
get_the_category( int $post_id = false ): WP_Term[]
Retrieves post categories.
Description
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() .
Parameters
$post_idint Optional-
The post ID. Defaults to current post ID.
Default:
false
Return
WP_Term[] Array of WP_Term objects, one for each category assigned to the post.
Source
File: wp-includes/category-template.php. View all references
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 );
}
Hooks
- apply_filters( 'get_the_categories',
WP_Term[] $categories ,int|false $post_id ) -
Filters the array of categories to return for a post.
Related
Uses
| Uses | Description |
|---|---|
| get_the_terms() wp-includes/category-template.php | Retrieves the terms of the taxonomy that are attached to the post. |
| _make_cat_compat() wp-includes/category.php | Updates category structure to old pre-2.3 from new taxonomy structure. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| get_the_category_list() wp-includes/category-template.php | Retrieves category list for a post in either HTML list or custom format. |
| the_category_ID() wp-includes/deprecated.php | Returns or prints a category ID. |
| the_category_head() wp-includes/deprecated.php | Prints a category with optional text before and after. |
| get_permalink() wp-includes/link-template.php | Retrieves the full permalink for the current post or post ID. |
| get_the_category_rss() wp-includes/feed.php | Retrieves all of the post categories, formatted for use in feeds. |
Changelog
| Version | Description |
|---|---|
| 0.71 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_the_category