category_description()
Retrieve category description.
Uses: term_description()
No Hooks.
Return
String
. Category description, if available.
Usage
category_description( $category );
- $category(int)
- Category ID.
Default: current category ID
Examples
#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.
#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>'; }
#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 ); }
#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() category description code WP 6.7.1
function category_description( $category = 0 ) { return term_description( $category ); }