get_the_category_by_ID()WP 0.71

Gets the category name by the given ID.

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

0

#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() 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 : '';
}