Maria’s Guides

Support and additional material for readers of books, articles, and digital media by Maria Langer.


Random Book Cover #1Random Book Cover #2Random Book Cover #3Random Book Cover #4Random Book Cover #5Random Book Cover #6Random Book Cover #7Random Book Cover #8Random Book Cover #9

How to Display Category-Specific Introductory Content

Posted on March 11th, 2007 at 1:07 pm ·
Filed in: RSS WordPress   

Another WordPress how-to.

The other day I got an e-mail message from a site visitor that was full of complements about my site. He especially liked the fact that different introductory content appeared for my Book Support categories. For example, if he was looking at the category for our WordPress book, he’d see the book cover and information about that book, as well as links related to that book. But if he looked at the category for my Mac OS book, he’d see information related to that title.

Two Category Archive PagesHow, he wanted to know, did I do that?

With conditional statements in my WordPress template files.

Why Bother?

First, let me take a moment to talk about why you might want to go through the trouble of customizing category archive pages.

In my case, my Web site combines information of interest to readers of my books (ie., book support) with my personal WebLog. Since some visitors come solely for book support, I needed to make them feel as if they’d reached their desired destination when they clicked a book support category link. I also had to assure them that they’d reached the pages for the specific book they’d come for.

This required two kinds of custom information:

  • A welcome message that explained the kind of information they could expect to get. I decided to put this information in the body of the page, right beneath the category heading.
  • Book specific information directly related to what they were promised in the book in the way of support, including links, downloadable files, etc. I put this in the sidebar, with book cover images to make it clear which book they were getting support for.

How could you use this on your site? Well, suppose your blog covers a variety of very different topics and you want to provide information for each topic on its category archive pages. Or suppose you’re using WordPress as a CMS and want to use categories for different product lines; you could have product line-specific content on each category archive page.

It Isn’t Difficult to Do…But it isn’t Exactly Easy, Either

Setting up different category information is as easy as inserting conditional statements in your WordPress template files with category specific information included.

WordPress 2 (Visual QuickStart Guide)Of course, if you’re not familiar with rolling up your sleeves to insert that code, it might not be easy for you. That’s why Miraz and I wrote our WordPress book. To explain things like that. And that’s why I won’t repeat what’s in that book here. (Sorry!)

With that said, here’s my code with an explanation of what it does and how it works. If you know how to modify your WordPress template files, you should have no trouble customizing this to meet your needs and inserting it on the proper places in your template files to get the same results.

In the Page Body

My site’s theme, a heavily modified version of Exquisite, includes a category.php file. This is the template that’s used whenever someone requests a category archive page by clicking a link to a category (as opposed to a single entry or date).

If your theme does not include a category.php file, it probably includes some code in an archive.php file or even the index.php file to specify what should appear on category archive pages. That’s what you should zero in on because that’s where this first bit of code needs to be inserted.

(Frankly, it’s a lot easier if you have a separate category.php page, so why not consider whipping one up? It’s great practice!)

<?php if (is_paged() ) { ?>
<!-- don't display the intro info -->
<?php } else { ?>
<?php if ( is_category('24') || is_category('25') || is_category('26') || is_category('27') || is_category('28') || is_category('29') || is_category('30') || is_category('31') || is_category('32') ) { ?>
<?php include (TEMPLATEPATH . '/langerbooksintro.php'); ?>
<?php } ?>
<?php } ?>

Let’s take it in sections.

<?php if (is_paged() ) { ?>
<!-- don't display the intro info -->

This determines whether the visitor is viewing something other than the first page of the category archive. If he is, all this stuff is skipped. I didn’t want the intro to appear at the top of every page for the category — just the first page.

<?php } else { ?>
<?php if ( is_category('24') || is_category('25') || is_category('26') || is_category('27') || is_category('28') || is_category('29') || is_category('30') || is_category('31') || is_category('32') ) { ?>

This says that if it isn’t paged (in other words, it is the first category page), which category is it? Is it one of the ones listed here by number?

<?php include (TEMPLATEPATH . '/langerbooksintro.php'); ?>

Well, if it is one of those, then display the contents of the file containing the intro (langerbooksintro.php). That’s a separate PHP file I created in the same directory to hold the book support introductory text. I like to keep my template files neat and this was my solution. I could also have replaced this statement with the information I wanted to appear on that first category page.

<?php } ?>

This closes up that second IF statement.

<?php } ?>

And this closes up the first one.

Pretty simple, no?

One thing to remember: the category archive page heading is outside all this conditional stuff. It’s set to display the category name and description as you see formatted in the illustration above. It’s like that on every page of my site.

In the Sidebar

I did pretty much the same thing for the sidebar, but I had to break it down into individual categories. Here’s my code:

Again, let’s take it in sections.

<?php if (is_home()) { ?>
<!-- <h2>Announcement</h2>
<ul>This is where I put announcements for the Home page only. It's currently commented out.</ul> -->

This starts a section I set aside for announcements I might want to appear at the top of the sidebar on the Home page. I commented it out because I’m not currently using it.

<?php } elseif ( is_category('24') ) { ?>
<!-- stuff for MAC OS VISUAL QUICKSTART GUIDE goes here -->
<?php include (TEMPLATEPATH . '/macosvqs.php'); ?>

If it’s not the home page, then is it the category page for category 24? If so, I want to insert specific content for that category. I do that by calling a separate file with that information in it. (This helps keep my sidebar.php file small.

I do this for a bunch of book support categories, one after the other. Each one calls a separate file with content I can modify at any time to update book covers, titles, etc.

<?php } else { ?>
<!-- stuff for everything else goes here -->

Here’s where I start to wind up the if statement stuff. I created a catch-all area for all pages that don’t meet the previous criteria — either the home page or one of several specific categories. If I wanted to include sidebar content for those pages, I’d put them after this. I don’t, so there’s nothing here. But I’m prepared!

<!-- Close All Conditional - THE FOLLOWING APPEARS ON EVERY PAGE -->
<?php } ?>

And finally, this is where I close the conditional statements. Everything after this appears for all pages.

Conclusion

You can use these basic constructions your your WordPress theme files to display different content for different categories or types of pages. It’s the same basic formulas, so to speak, with different ingredients. Doing this makes it possible to completely customize your pages based on content.

Try it for yourself! Just remember to keep backup copies of all of your modified theme files — just in case you mess up and need to start over again.

Good luck!

 • Read 1532 Times
Add to Del.icio.usAdd to Del.icio.us • Technorati ThisTechnorati This • Digg ThisDigg This • Stumble ItStumble it! • Twit ThisTwit This


 

 

3 responses so far ↓

  • 1 Doug Smith // Apr 20, 2007 at 2:45 pm

    It can be even easier if you only need to display just a little blurb of text at the top of a category page. Enter that text into the category description field in Wordpress. Then, one little line of PHP in your template will spit out that description.

    The archive.php template in most themes already have a condition to check if a category archive is being viewed. Just enter this line under that condition:

    It will probably need to be wrapped in paragraph tags or whatever markup needed for your theme.

  • 2 Doug Smith // Apr 20, 2007 at 2:48 pm

    Sorry, Wordpress killed the part of my example wrapped in php tags. The line you need is this:

    echo category_description();

    but wrapped in a php tag, of course.

  • 3 bekabug // Mar 28, 2008 at 7:56 pm

    I can’t believe this post doesn’t have 200 comments; I have been looking for this solution for days and days.

    Thank you :)

    bekabug’s last blog post: Thoughtful Thursday : Spring

Leave a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

If you have a Gravatar, it will appear beside your approved comment.
No Gravatar? Get one free!