get_cat_name()WP 1.0.0

Gets the name of the category by the passed ID.

Uses: get_term()

No Hooks.

Return

String. Category name, or an empty string if the category doesn't exist.

Usage

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

Examples

#1 Display the name of category 4

<?php echo get_cat_name( 4 );?>

#2 Let's get the name of category 4 into a variable for further processing

// get
$catname = get_cat_name( 4 );

// use in line
echo 'You are in category: '. $catname;

#3 Get the name of the current category

If we are on the archive page of a category, then the name of the current category can be obtained as follows:

// if we are on the category page
if( is_category() ){
	echo get_queried_object()->name;
}

Changelog

Since 1.0.0 Introduced.

Code of get_cat_name() WP 5.9.3

function get_cat_name( $cat_id ) {
	$cat_id   = (int) $cat_id;
	$category = get_term( $cat_id, 'category' );

	if ( ! $category || is_wp_error( $category ) ) {
		return '';
	}

	return $category->name;
}