One of the fundamental building blocks of the WordPress blog structure is the category. Of course the category serves as a way to organize the content and help visitors with navigation, but it also provides theme developers with opportunities to do some different things to make the theme a bit more distinct or more effective.
In this post we’ll take a look at a number of things you can do with WordPress coding that involve categories in one way or another. Hopefully this will be a helpful guide that you can refer to whenever you’re developing a new theme.
Exclude Specific Categories from Your Category List:
Most blogs include a list of categories in the sidebar to help visitors find the content that they want. However, there may be times when you want to exclude certain categories from the list. Fortunately it’s a very simple change.
This is the code to display all categories.
<?php wp_list_cats(); ?>
This is the code to exclude categories number 4 and 5 (you can get the category numbers in from the WP dashboard).
<?php wp_list_cats('exclude=4, 5'); ?>
Remove a Category from the RSS Feed:
You may also want to exclude posts in a specific category from being included in your RSS feed. Sideblogs are increasingly popular and usually you’ll want to exclude these posts from the main feed. Again, excluding categories is pretty easy. You could always use a plugin like Advanced Category Excluder (http://wordpress.org/extend/plugins/advanced-category-excluder/), or you could make the changes manually.
To exclude a category, use this URL for your feed:
http://example.com/feed?cat=-4
That would exclude category number 4. To exclude multiple categories, use a URL like this:
http://example.com/feed?cat=-3&cat=-4&cat=-5
Exclude Posts in a Category from Appearing on the Homepage:
There may be situations where you want to exclude posts from the front page based on their category. Often this will be in the same situations where you also want to exclude it from the feed.
To exclude a category from the loop on the front page, find this code:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
and change it to this:
<?php query_posts('cat=-4'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
This will exclude posts in category number four from being displayed on the front page.
Show Links to the Most Recent Posts from a Specific Category in the Sidebar:
Many blogs use simple WordPress coding to show links to the most recent 5 or 10 posts in theĀ sidebar. You can adapt this to include or exclude specific categories.
To show the 10 most recent posts in category number 4, use this code:
<?phpquery_posts('cat=4&showposts=10'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>
To exclude category number for, simply put a minus sign (-) in front of the category number, like this:
<?phpquery_posts('cat=-4&showposts=10'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>
The code above will show the 10 most recent posts from all categories except number four. To exclude multiple categories simply add a comma, like this:
<?phpquery_posts('cat=-4,-5&showposts=10'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>
Set Up a Separate Feed for a Category:
If you exclude a category from your main feed you may also want to offer a separate feed for the one excluded category (again, this is common for sideblogs). WordPress will easily give you the feed for any one category by using a URL like this one:
http://example.com/category/nameofyourcategory/feed/
If you want, you can take that URL and run it through FeedBurner and treat it like any other feed.
Show Post Excerpts on Category Pages:
To avoid duplicate content and to reduce the length or category pages, many bloggers choose to show excerpts instead of full posts. It’s a simple change. Find this code:
<?php the_content() ?>
And change it to this:
<?php the_excerpt() ?>
Show Only Post Titles on Category Pages:
Another option is to only show post titles. You can do that by using this code:
<?php the_title() ?>
Most likely your theme already has this code surrounded by a link to the permalink of the post. So what you will want to do is just delete the code that calls the excerpt or the content and leave the title and link intact.
Hopefully this post gave you some ideas that you can implement in your own themes.












Nice breakdown of the category options for wordpress.
Sidenote: Your blockquotes are stretching about 10 pixels inside your right sidebar.
Mike,
You’re fast. I thought I could fix the width before anyone saw it.
thanks for sharing. is very useful
Nice tut to read first thing in the morning. Further examples of how easy it is to fiddle and customise Wordpress with little understanding of php.
Thanks!
Nice tips, really took my interest for some time
Another thing people might want to check out regarding category template presentation is my Idealien Category Enhanceents plugin. It enables you to have the same capability of customization of templates and selection through the admin console for categories that you have for page templates.
Instead of setting up a specific category-5.php you can specify a named title (category-photos.php) and then select which categories use that through the manage > categories panel. This also enables you to have custom post templates (single-photos.php).
Good list. I could have really used this a few months ago. I just wanted to mention (since I had to figure it out the hard way) that when you mess with query_posts(), as in the “Exclude Posts in a Category from Appearing on the Homepage” section, that “WordPress then ignores the other parameters it received via the URL (such as page number or category)”, so then the “previous/next page” links are broken.
I wanted to include a possible fix here to save anyone else the headache. Just grab the ‘paged’ var, and add it to the call to query_posts() like so:
<?php query_posts(”paged=$page&cat=-7″);
Whoa…seems like comments mangled my code. Should have had 2 lines:
1.
$page = (get_query_var(’paged’)) ? get_query_var(’paged’) : 1;
2.
query_posts(”paged=$page&cat=-7″)
Jamie,
Thanks for sharing.
Ryan,
Thanks for the heads up and for providing the code.
[…] Category Hacks for WordPress Theme Designers | Vandelay Website Design (tags: wordpress) […]
What a great list of hacks. Categories are the biggest hassle and can get out of control after a blog reaches a certain point.
Thanks for sharing.
[…] Via | Vandelay Design […]
Category Hacks for WordPress Theme Designers…
One of the fundamental building blocks of the WordPress blog structure is the category. Of course the category serves as a way to organize the content and help visitors with navigation, but it also provides theme developers with opportunities to do som…
[…] Category Hacks for Wordpress Theme Designers at Vandelay Design. […]
Could you please help with this?
I have a multi-user WordPress blog. In writing and publishing a post, the user can choose more than one categories for one post to go in. How can I tweak it so the user can choose only one of the categories available for their level? In other words, when the user checks one category to post, other categories in the list (in the write post window) will be unchecked (kind of choosing only one correct answer in a survey). Please help. Thank you very much.
[…] Category Hacks for WordPress Theme Designers (VandelayDesign) […]
[…] Category Hacks for WordPress Theme Designers […]
Hello
In your “Show Only Post Titles on Category Pages:” when i tried to delete excerpt and the_content and only leave the title, it will display the number of posts i set in the settings of wordpress. What if I want to display 50 titles in that category but in my wordpress settings, i only display 5 posts in my home?
ems,
I believe if you use part of the code from the section above where I show how to display posts from a specific category in a certain location, there is a spot in that code that says show x number of posts. I think if you put that in the code you’ll be able to control how many are shown. I’m not in a spot where I can test it right now, but that’s what I would try.
You’re a life-saver, I’ve been looking for this for the last hour - how to exclude posts from a category. Its working well. Dave.
Thanks for showing how to create category feeds - one additional thing though… how does one make a feed that shows ALL the posts within that category. For the main feed I have set it to show 10 posts from the Settings page. But for the category feeds I want to show all the posts from that category.
jcsot,
I’m not sure if I understand your question or not. Are you talking about the feed as it will appear in an RSS reader? It sounds like you’re referring to the loop on a category page. This tutorial may help.
[…] Category Hacks for WordPress Theme Designers […]
[…] Category Hacks for WordPress Theme Designers […]
wp_list_cats (Deprecated in 2.1)
wp_list_cats is deprecated since WP 2.1
please use wp_list_categories
Monika
[…] 46. Category Hacks for WordPress Theme Designers […]
[…] 46. Category Hacks for WordPress Theme Designers […]
[…] 46. Category Hacks for WordPress Theme Designers […]