Since I’m running the new podcast from within a category here, I decided it was time to make some long-delayed tweaks to the category archive pages. Some of this information was surprisingly hard to come by, so I thought I’d share it here for fellow WordPress users (note that this only applies to independently hosted sites, not those on WordPress.com).
1. Displaying an RSS button next to the category title
All WordPress categories have their own feeds, but if you want to give your readers the ability to subscribe to them, you have to display them somewhere. I can easily envision someone being only interested in my Nature/Ecology or Photos categories, to the exclusion of everything else. But since I [used to] list my categories in a nifty slide-down menu (click the Browse link in the navigation bar), I couldn’t work the RSS links into a list, which is the usual approach. Why not display them at the head of each category archive page instead?
First I downloaded the standard orange RSS feed icons, uploaded the smaller one (14×14 pixels) to Via Negativa’s media library, and copied and pasted the URL into Notepad. Then after considerable searching, I found the requisite code, added the icon link in the appropriate spot, and placed it one space to the left of the bit that calls up the category title in category-archive.php
. Here’s what I ended up with:
<?php
$this_category = get_category($cat);
print '<a href="'.get_category_feed_link($this_category->cat_ID, '').'"><img src="http://yoursite.com/wp-content/ ... /feed-icon-14x14.png"></a>'; ?> <?php single_cat_title() ?>
2. Adding the category description to the first page of the archive
You might’ve noticed that the categories section of the WordPress dashboard (left menu, Posts drop-down) allows you to edit each category to enter a description. These descriptions can even contain links — see for example my Photos category, where I’ve just added links to my photoblog and Flickr page. Most blog themes leave descriptions out, but they’re a great feature, I think. I don’t know why I didn’t do this a long time ago. Here’s the code:
<div class="category-description"><?php if ( $paged < 2 ) { ?>
<?php echo category_description( $category ); ?>
<?php } else { ?>
<?php } ?></div>
The conditional statement restricts it to the first page of the archive. The div definition of course can be anything you want, and one look at my current styling will tell you that I am not someone you want to consult on design.
I’ve barely begun adding descriptions to categories, and probably won’t add descriptions to all of them, but it’s looking like a good way to add additional information or important qualifications. And I just realized that the new descriptions appear as mouse-over text in this theme’s Browse menu, too. Far out.
3. Styling categories
The above tips barely scratch the surface of what’s possible. To create different styles for each different category archive, you have to create a new template file for each category and upload it to your theme. This involves copying and pasting the code from the main category.php
into a text editor, then naming the file something like category-7.php
, where 7 is the number id of that category. Finding that number is the only tricky part. In the categories section of the dashboard, move your cursor over the category link and look in the status bar at the bottom of your browser. At the end of the string, where it says cat_ID=x
— that’s your number.
With new category template files in place, it’s just a matter of defining the CSS styles in the stylesheet and modifying the files accordingly. It’s not something I plan on doing, but I include it here for the sake of completeness. (Please note that if you want individual styles for posts on the index page according to category, that’s an entirely different procedure.)
4. Adding custom sidebar text to categories
This is another thing I’m unlikely to implement here, but it seems really useful for magazines and other not-so-bloggy websites. There are a growing number of plugins to apply this and other CMS functions, but not being a real geek I’m not really qualified to evaluate them, so you should really look elsewhere for authoritative assessments. (Not that you’ll find any: good information on WordPress is hard to find, lost in a sea of search-engine bait.) For what it’s worth, though, we’ve been using the new WP Custom Widget for qarrtsiluni’s first online chapbook (to add publication history notes in the sidebar of pages like this one), and it’s easy to use and still works fine, knock wood. So if I wanted to add custom text to category sidebars, I guess that’s where I’d start. The real way to do this is with conditional tags, but that requires some pretty gnarly coding — it’s not easy copy-and-paste stuff as in the examples above.