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.
How, 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.
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!)
Let’s take it in sections.
This closes up that second IF statement.
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:
<!-- stuff for everything else goes here -->
<!-- Close All Conditional - THE FOLLOWING APPEARS ON EVERY PAGE -->
Again, let’s take it in sections.
<!-- 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 -->
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!
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.
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.
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
[...] this in WordPress can be somewhat tricky. Maria’s Guides has a great tutorial on how to create category specific archive pages. This is probably the best option if you do not want to bother with the other methods available. [...]