get_the_category_by_ID()
Gets the category name by the given ID.
Uses: get_term()
No Hooks.
Returns
String|WP_Error. A string (category name) or false (if the category could not be found).
Usage
get_the_category_by_ID( $cat_ID );
- $cat_ID(integer) (required)
- The ID of the category whose name needs to be retrieved.
Examples
#1 Let category 7 be named "Author Functions". Get this name by ID:
$cat_name = get_the_category_by_ID( 7 ); echo $cat_name; // outputs: Author's Functions
Changelog
| Since 0.71 | Introduced. |
get_the_category_by_ID() get the category by ID code WP 6.9
function get_the_category_by_ID( $cat_id ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
$cat_id = (int) $cat_id;
$category = get_term( $cat_id );
if ( is_wp_error( $category ) ) {
return $category;
}
return ( $category ) ? $category->name : '';
}