How to display category description in WordPress

Posted in WordPress | Tagged , , , | Comments Off

As you may have noticed in your WordPress blog, the description of a category by default is not displayed anywhere. Although not that many themes use this feature, writing quality description for your categories have some beneficial effects. It will not only boost SEO of the category archives, but will also improve user experience by displaying relevant information and complementing generally short category name.

First of all you have to start with creating a new category in your WordPress site.

  1. Login to your WordPress website
  2. Click on Posts (top left hand side corner)
  3. From the menu chose Categories
  4. In field Category Name type in name that will identify your new category
  5. Chose Category Slug – URL friendly version of that name
  6. In Category Parent decide if this is going to be one of the main categories or a subcategory
  7. Finally write a Description of your new category
  8. Click on Add Category to save your work.

Note: If you already have a category and would only like to add its description, in step 5 click on the name of your category and edit Description field. Then save.

Once you have a new category setup, what you have to do next is to tell WordPress to display that data.

It is very easy thing to do and requires only one line of code.

<?php echo category_description(); ?>

But you have to put it in correct place, and in right file.

  1. Go to your theme folder (wp-content > themes > name of your theme >)
  2. Open archive.php file
  3. Find these lines of code (usually line 15 and 16)
<?php /* If this is a category archive */ if (is_category()) { ?>

 <h2 class="pagetitle">Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>

4) Insert <?php echo category_description(); ?> after closing </h2> tag.

Your code should look like this:

<?php /* If this is a category archive */ if (is_category()) { ?>
<h2 class="pagetitle">Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>

<?php echo category_description(); ?>

5) Save and upload the file.

That’s all. Now when you view your category page, its description should be displayed below the heading – the category name.

What you may like to do next is to change wording a bit and apply CSS style.

For example:

<?php /* If this is a category archive */ if (is_category()) { ?>

<div id="page_title"><h1><?php single_cat_title(); ?></h1>

<?php echo category_description(); ?>

Note: you will have to update your style.css file and create id selector #page_title, then style h1 tag – contains category title, and p tag – contains category description.

CSS code:

#page_title    { margin:0 0 40px 0; }

#page_title h1 { color:#222; font-weight:normal; }

#page_title p  { color:#fff; background:#333; font-size:0.9em; }

If you would like to style it like in the image below you will also have to set margin and padding.

how to display category description in WordPress
I hope you enjoyed this short tutorial and found it useful.

Comments are closed.