Per Category Intro Text

By Alex on May 12 2008 | Listed under Tutorials, WordPress | 10 Comments

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.

1
2
3
4
5
6
7
8
<?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.

31 Days to Build a Better Blog Affiliate Link

Did you enjoy this post? If you did, so might others! Please share it!

10 Comments - Add Yours!

  • Trisha Cupra, Web Design Watchdog wrote on May 20 2008 at 4:43 pm | Permalink

    Where do I put the code? In the archive.php?

  • Alex wrote on May 20 2008 at 5:47 pm | Permalink

    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

  • Jasonian. wrote on June 21 2008 at 6:09 pm | Permalink

    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….

  • Alex wrote on June 21 2008 at 6:29 pm | Permalink

    @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! :-)

  • Mauro, Suitecom Hotel Marketing wrote on June 25 2008 at 11:34 am | Permalink

    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?

  • Mauro, Suitecom Hotel Marketing wrote on June 25 2008 at 11:52 am | Permalink

    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!

  • Trisha Cupra, Web Design Watchdog wrote on June 29 2008 at 3:19 pm | Permalink

    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?

  • Mali wrote on December 18 2009 at 3:53 pm | Permalink

    Great Post Alex, and works a treat, but sadly im not a coding man and just wondered how i could add some style to the text? Any help would be much appreciated.

    Once again cheers for the help

    • Alex wrote on December 22 2009 at 2:12 am | Permalink

      Hi Mali, you can add some styling to the text by adding a class or id to the <p> tags, like <p class="category_desc"> and then in your CSS, add

      .category_desc {
         padding: 5px;
         border: 1px solid #CCCCCC;
      }

      Hope that helps.

      • Mali wrote on January 13 2010 at 1:20 pm | Permalink

        Big thanks for the reply. Sorry for the delay but Christmas and all that :D Hope you had good holidays. So far so good the description works great but now ive got a fussy client asking if they can add the text them selves Doh Not letting them touch the back end.

        Cheers
        Mali

Post a Comment

Your email is never published nor shared.

*
*
User Gravatar