If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
Most blogs that use WordPress are set up so that their posts fall under different categories, and all these posts can be listed on one category page. But what if you wanted to add some kind of description about that category when viewing only those posts?
A category page is set up so that whatever category you are viewing, only those posts are seen, but only one file is needed in your theme to do this. That file is usually called category.php, but if that is missing, then archive.php is used, and if that is not there either, then finally index.php is used.
It is possible to create an infinite amount of different category pages by appending the id of that category to the file name, like category-6.php. You could then add some text to each of those files, describing each category.
But isn’t that a lot of work? Why not do all of that in one file? By using the is_category() function it is possible to do that. We can add the following outside of the loop, so that it isn’t repeated per post.
<?php if (is_category('Category A')) { ?>
<p>This is the text to describe category A</p>
<?php } elseif (is_category('Category B')) { ?>
<p>This is the text to describe category B</p>
<?php } else { ?>
<p>This is some generic text to describe all other category pages,
I could be left blank</p>
<?php } ?>
This does the following. Checks to see if we are looking at Category A, if we are then show the first bit of text, but if we’re not then check if we are looking at Category B. If we are then show that bit of text, and finally, if it is neither Category A or B, then show this default text.
You can keep adding an elseif for as many different category texts as you need.


Where do I put the code? In the archive.php?
If there is a category.php in your theme, then use that, if not, then yeah, use the archive.php.
Post back if it works for you, or if you have any further questions!
Alex
The WordPress.org Codex makes the same suggestion as you do, and I don’t understand why.
http://codex.wordpress.org/Category_Templates#Adding_Text_to_Category_Pages
Categories in WordPress have a “description” field (see your Manage > Categories admin page). Instead of having to write code to check each category, just display that built-in description.
http://codex.wordpress.org/Template_Tags/category_description
I have a feeling I’m missing an important aspect of usage….
@Jasonian - It suggests the same thing in the codex because it was me that added it! The suggestion that that section starts with is that you could create a different category.php file for each category, adding different text at the top. This is of course a lot of work, so I added a dynamic version, allowing for one category.php file.
As per your second point, you could of course use
<?php echo category_description(); ?>in your category.php file, however this would then always output the description, regardless of whether you wanted one or not. Using this method allows for you to define some text for only certain categories.I hope that answers your query!
Hi,
very useful!
But if I have many categories (for example 50) is not convenient to have a long list of “elseif.” Maybe is there a more elegant way?
I resolved. I have done so:
1. I created a file (catintro.php) where I placed the code
2. Then I have inserted with a “php include” in category.php
I hope this help someone
Ciao!
Is there a way to write the PHP code so that it checks if there is something written in the description field for the category, and if there is then it displays it, but if it is blank it disregards it?