category_description()WP 1.0.0

Gets the description of the category, which is specified on the category editing page in the admin panel (posts > categories).

If category_description() is used on an archive page other than category.php, then before using the function, you need to check that the category page is being generated, not some page of tags, authors, dates, etc. This can be done with the conditional tag is_category().

No Hooks.

Returns

String. Category description.

Usage

$description = category_description( $category_id );
$category_id(int)
ID of the category whose description needs to be retrieved.
Default: current category (in the query)

Examples

0

#1 A common example of use

Let's display the description of category 3 (category id) using the echo operator. echo is needed to display the category description because the function just returns the description and doesn't display anything.

<?php echo category_description(3); ?>

As a result, we get a description of category 3.

0

#2 Output the category description only if there is one

$cat_desc = category_description();

if ( $cat_desc ) {
	echo '<div class="cat__desc">'. $cat_desc .'</div>';
}
else {
	echo '<div class="no__cat__desc">No description!</div>';
}
0

#3 Description of the category received through the label

Let's display the category description using a slug (an alternate category name) instead of an ID as in the last example. To do this, get the ID using get_category_by_slug().

$term = get_category_by_slug( 'category-slug' );

if( $term ){
	echo category_description( $term->term_id ); 

	// or
	echo esc_html( $term->description );
}
0

#4 Clean up the category description in the output, through the filter term_description

Suppose our category description uses the shortcode [image=/link_to_image] we use this shortcode in other places, but when we display the category description, we don't need it. To remove it, we will use the filter:

add_filter( 'term_description', 'clear_term_description_image_shortcode' );

function clear_term_description_image_shortcode( $value ){

	return preg_replace( '/[image=[^]]*]/', '', $value );
}

Changelog

Since 1.0.0 Introduced.

category_description() code WP 6.8.3

function category_description( $category = 0 ) {
	return term_description( $category );
}