Hack: Display Category Descriptions On Archive Pages

So when someone ends up at an archive page on your site, like /category/something, they get a page title followed by a list of posts. That’s fine for a lot of purposes. But what if you wanted to put some text at the top of that? Maybe something that describes the category, explaining what sort of posts the reader is now viewing.

For instance, if you have a photography site, and you have a category called cityscapes, you may want to have something appear the top of /category/cityscapes explaining how these are photographs of urban landscapes, etc.

You can already go into your Categories list in the WordPress admin, edit a category, and then add a Description to that category. So this is a good place to put that text.

Next, add the following code to your custom theme or child theme. Put it in the archive.php file, right below the h1 closing tag so it appears just below the title.

<? $catdesc = category_description(get_queried_object_id());
if (strlen($catdesc) > 0) {
    echo('<div class="catdesc">'.$catdesc.'</div>');
}
?>

All this does is insert a div element with the description of the category, if the description exists. I also added a CSS class, so you can style it.

You can do the same for tags too. They have descriptions you can edit in the admin, and then use the tag_description function to get that value.

One Reply to “Hack: Display Category Descriptions On Archive Pages”

Leave a Reply

Your email address will not be published. Required fields are marked *