How to display category description in WordPress with category_description function
The WordPress category_description
function retrieves the description of a category and displays it on the category archive page. This can be useful for providing additional information about the category to visitors, such as its purpose or the types of content it contains.
By using the category_description
function, website administrators can easily manage and update the descriptions for each category without having to manually edit the category archive page template.
WordPress category_description Function Parameters
The category_description
function accepts the following parameters:
$category
(int, optional): Category ID. Defaults to the current category ID.
Value Returned by the category_description Function
The category_description
function returns a string containing the category description, if available.
Examples
How to display the category description on a category archive page
<?php
$category_id = get_query_var('cat');
$category_description = category_description($category_id);
if (!empty($category_description)) {
echo '<p>' . $category_description . '</p>';
}
?>
This code snippet retrieves the category ID of the current category archive page using get_query_var
, then uses the category_description
function to get the description of that category. If the description is not empty, it is displayed within a paragraph tag.
How to display the category description on a single post page
<?php
$category = get_the_category();
$category_description = category_description($category[0]->cat_ID);
if (!empty($category_description)) {
echo '<p>' . $category_description . '</p>';
}
?>
This code snippet retrieves the category of the current post using get_the_category
, then uses the category_description
function to get the description of that category. If the description is not empty, it is displayed within a paragraph tag.
How to display the category description on a custom category page
<?php
$category_id = 5; // Replace with the desired category ID
$category_description = category_description($category_id);
if (!empty($category_description)) {
echo '<p>' . $category_description . '</p>';
}
?>
This code snippet retrieves the description of a specific category using the category_description
function and displays it within a paragraph tag. The category ID is manually specified in this example, but it can be dynamically obtained based on the specific use case.
Conclusion
The category_description
function is a valuable tool for organizing and categorizing content on a website. By allowing for the addition of descriptive text to categories, it enhances the user experience and helps to provide context for the content within each category. Additionally, the function’s flexibility and ease of use make it a valuable asset for developers and website administrators. With its ability to improve navigation and user engagement, the category_description
function is a key feature for any website that relies on categorized content.
Related WordPress Functions
- Using single_cat_title to get the current category title in WordPress
- How to display the category of a post in WordPress using the_category
- How to display a list of WordPress categories with wp_list_categories
- How to retrieve the post category in WordPress using get_the_category
- Getting WordPress categories using get_categories function