Function and usage of category_description() in wordpress


Answers ( 1 )


    The category_description() function in WordPress is designed to retrieve the description of a category. Here's a detailed explanation of this function and an example of how to use it:

    Function Signature:

    category_description( int $category ): string


    • $category (int, optional): This parameter specifies the ID of the category for which the description is to be retrieved. It's an optional parameter. If not provided, WordPress will use the ID of the current category.

    Return Value:

    • The function returns a string, which is the description of the specified category. If there's no description available for the category, or if the category does not exist, it returns an empty string.

    Usage Example:


    Let's say you have a category with the ID 5, and you want to display its description on your website.

    Sample Code:

      // Get the category ID. In this case, we're using '5'.
      $category_id = 5;
      // Retrieve the category description.
      $description = category_description($category_id);
      // Check if the description is available and output it.
      if (!empty($description)) {
          echo '<div class="category-description">' . $description . '</div>';
      } else {
          echo 'No description available for this category.';

    In this example, the function category_description($category_id) is used to get the description of the category with ID 5. If a description exists, it is printed within a div element. If there's no description available, a message is displayed instead.

    This function is particularly useful for theme developers who want to add contextual information about categories on category archive pages or in widgets. It helps to provide more insights into what the category is about, enhancing the user experience on a WordPress site.

Leave an answer