get_cat_name()WP 1.0.0

Gets the name of the category by the passed ID.

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

0

#1 Display the name of category 4

<?php echo get_cat_name( 4 );?>
0

#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;
0

#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.

get_cat_name() code WP 6.4.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;
}