get_the_category_by_ID()WP 0.71

Retrieve category name based on category ID.

No Hooks.

Return

String|WP_Error. Category name on success, WP_Error on failure.

Usage

get_the_category_by_ID( $cat_id );
$cat_id(int) (required)
Category ID.

Examples

0

#1 Let category 7 be named "Author Functions". Get this name by ID: [auto-translate]

$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.5.3

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