How to Display Category-Specific Introductory Content

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!)
<?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:
<?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> -->
<?php } elseif ( is_category('24') ) { ?>
<!-- stuff for MAC OS VISUAL QUICKSTART GUIDE goes here -->
<?php include (TEMPLATEPATH . '/macosvqs.php'); ?>
<?php } elseif ( is_category('25') ) { ?>
<!-- stuff for EXCEL VISUAL QUICKPROJECT here -->
<?php include (TEMPLATEPATH . '/excelvqpj.php'); ?>
<?php } elseif ( is_category('26') ) { ?>
<!-- stuff for EXCEL VISUAL QUICKSTART here -->
<?php include (TEMPLATEPATH . '/excelvqs.php'); ?>
<?php } elseif ( is_category('27') ) { ?>
<!-- stuff for QUICKBOOKS VISUAL QUICKSTART GUIDE here -->
<?php include (TEMPLATEPATH . '/quickbooksvqs.php'); ?>
<?php } elseif ( is_category('29') ) { ?>
<!-- stuff for WORD VISUAL QUICKPROJECT GUIDE here -->
<?php include (TEMPLATEPATH . '/wordvqpj.php'); ?>
<?php } elseif ( is_category('30') ) { ?>
<!-- stuff for WORD VISUAL QUICKSTART here -->
<?php include (TEMPLATEPATH . '/wordvqs.php'); ?>
<?php } elseif ( is_category('31') ) { ?>
<!-- stuff for WORDPRESS VISUAL QUICKSTART GUIDE here -->
<?php include (TEMPLATEPATH . '/wordpressvqs.php'); ?>
<?php } elseif ( is_category('32') ) { ?>
<!-- stuff for OUT-OF-PRINT BOOKS here -->
<?php include (TEMPLATEPATH . '/oopbooks.php'); ?>
<?php } else { ?>
<!-- stuff for everything else goes here -->
<!-- Close All Conditional - THE FOLLOWING APPEARS ON EVERY PAGE -->
<?php } ?>
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!
How I Use the Readers Post Plugin

In recent comment on my article about WordPress 2.1 Plugin Compatibility, Julie said that she was having trouble getting the Readers Post plugin to work. I added this plugin to my site a few months ago and wrote about it here. That piece didn’t include any how-to information.
What It Does
The Readers Post plugin by Stefan Groenveld is a three-trick pony that offers the following features:
readers()returns a count of the number of people who have read a post.last_posts()displays a list of the most recently read posts.hot_posts()displays a list of the post popular posts, including a reader count.
I use all three of these options on my site. But rather than go into detail about the parameters — you can find that on the plugin’s page; scroll down for English — I’ll provide the code I use as an example.
readers()
This function must be used within The Loop. (If you don’t know what the Loop is, look it up in the WordPress Codex or, better yet, buy our WordPress book, which explains it in detail.) I use it in my post “footer” to display the number of times a post has been read along with a bunch of other stuff. Here’s the code snippet for readers():
<?php if(function_exists('readers')) {readers('Read ',' Times'); } ?>
This code does two things:
?php if(function_exists('readers'))checks to see if the Readers Post plugin is installed. (Actually, it checks to see if thereaders()function is available.) If it is available,{readers('Read ',' Times'); }displays the results of thereaders()function with the simplebeforeandafterparameters I’ve provided.
The result looks something like this — I put a red box around the code’s actual output:

last_posts()
I use last_posts() in the sidebar to list the 8 most recently read posts. I find this list of posts fascinating. Each time I visit the site, I look at the list to see what people have been reading. Sometimes I’ll see an old post listed and click the link to revisit it myself. I hope other people find some of the titles intriguing and visit some of my older posts, too.
Anyway, the code I use is as follows:
<?php if (function_exists('last_posts')): ?>
<?php last_posts(8); ?>
<?php endif; ?>
This does the same thing as the code listed above for readers(), but it uses a different syntax for the conditional (if) statement. 8 is the parameter for the number of posts to display.
On my site, this code is enclosed within <ul> and </ul> tags for formatting — most templates use those tags to present information in the sidebar, but your theme may be different. I also put a heading over it to explain what it is.
The result, at this very moment, looks like this on my site’s Home page:

hot_posts()
I also use hot_posts() in the sidebar. It lists the 8 most popular posts — the ones read most. Keeping in mind how much I love stats, you can imagine how much I like this information. To me, it’s like a horse race, with certain posts racing to have the most hits. When a new post passes an old one, I’m always kind of tickled. (Yeah, I know what you’re saying. What a geek!)
Anyway, here’s my code:
<?php if (function_exists('hot_posts')): ?>
<?php hot_posts(8); ?>
<?php endif; ?>
Again, this code is enclosed within <ul> and </ul> tags for formatting. And since the Readers Post plugin has no way to count posts read before it was installed, I also included a note in small type that indicates when the counts started. I’ll probably remove that count sometime in the future. Here’s what it looks like on my site’s Home page:

Some Tips
If your blog doesn’t get many hits (yet), I don’t recommend using readers() or hot_posts(). The numbers they display might be depressing. I think (but am not sure) that the plugin starts counting as soon as it’s installed and activated, so you could always add these two functions at a later time and have counts displayed from the installation day rather than the first day the function is used. And site administrator visits are not counted, so you can re-read any post you like without skewing the count.
last_posts() will always be a great addition to the site, almost like a “suggested reading” list for visitors — and a re-reading list for you.