Vandelay Design

Category Hacks for WordPress Theme Designers

Published September 14th, 2008 in WordPress

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:

&lt;?php the_content() ?&gt;

And change it to this:

&lt;?php the_excerpt() ?&gt;

Show Only Post Titles on Category Pages:

Another option is to only show post titles. You can do that by using this code:

&lt;?php the_title() ?&gt;

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.

If you like getting design inspiration by looking at excellent blog designs, you’ll love our new gallery site Blog Design Heroes.

98 Responses to “Category Hacks for WordPress Theme Designers”

Mike Smith September 14th, 2008

Nice breakdown of the category options for wordpress.

Sidenote: Your blockquotes are stretching about 10 pixels inside your right sidebar.

Vandelay Design September 14th, 2008

Mike,
You’re fast. I thought I could fix the width before anyone saw it.

Mike Smith September 14th, 2008

:) I just happen to open my reader to check for some articles for some design inspiration and saw your post.

NaldzGraphics September 15th, 2008

thanks for sharing. is very useful

Nathan Beck September 15th, 2008

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!

Dainis Graveris September 15th, 2008

Nice tips, really took my interest for some time :)

JamieO September 15th, 2008

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).

Ryan September 15th, 2008

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″);

Ryan September 15th, 2008

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″)

Vandelay Design September 15th, 2008

Jamie,
Thanks for sharing.

Ryan,
Thanks for the heads up and for providing the code.

Bryan September 16th, 2008

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.

Long Vit September 26th, 2008

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.

ems October 7th, 2008

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?

Vandelay Design October 7th, 2008

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.

Sugar Web Development October 7th, 2008

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.

jcsot October 12th, 2008

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.

Vandelay Design October 12th, 2008

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.

Monika December 2nd, 2008

wp_list_cats (Deprecated in 2.1)

wp_list_cats is deprecated since WP 2.1

please use wp_list_categories

;)

Monika

Simon January 6th, 2009

Hey!

This was a great help. I was thinking that maybe you should incorporate Ryan’s hint, it saved me from an immense headache.

loren January 10th, 2009

After two days looking for a way to display a list of posts filtering by category finally the solution. I’m not an expert but i guessed there would be a solution
Thank you very much

Just advise that there’s a missing space between php and query

so the right code is:

<a href=”">

loren January 10th, 2009

i had problems with the code in top-header.php because affects the rest of the pages, works fine in sidebar.php
:-?

Gary Jones January 13th, 2009

Total newb to WP and struggling with the concepts. Maybe what I want to do simply isn’t possible, but I have to ask. I want certain categories and posts to only appear on certain pages. Is this in any way possible? I’m creating a business directory, so there will be hundreds of categories, posts and comments related to the specific section of the directory. To need to exclude these from other pages with be a major task. Thanks.

Vandely Design January 13th, 2009

Hi Gary,
Excluding categories is certainly possible, but I’m not sure I exactly understand what you’re asking. The term “pages” in WordPress lingo has a very specific meaning (i.e. a post is not a page), so I’m not sure I understand your situation. One thing I’ll definitely point out to you is the Advanced Category Excluder Plugin – http://wordpress.org/extend/plugins/advanced-category-excluder/

This is the easiest way to control which categories of posts appear in certain places.

Gary Jones January 13th, 2009

Maybe it’s just my lack of understanding of WordPress here. If I can give one example, my main page has a menu which has a business directory, with dropdown lists, for example, to Real Estate. This then drops down to links to, for example, For Sale, which is a page. I have a category setup for Real Estate – For Sale. I then want to post an entry for a listing, with a picture and allow users to comment or query this specific listing. It would be good to be able to also select the primary Real Estate category as well and have the post showup on the main index Real Estate page, with a thumbnail and a link to the specific detailed posting on the subpage.

In the same manner I would like users to be able to post their own listings on my Classifieds section.

I guess the gist of what I’m needing is that certain categories only show on certain pages. Does that make sense? Ideally I would like specific pages for each section, pages that would show different ads, video content and links. Impossible?

Thanks.

Vandely Design January 13th, 2009

Gary,
By using parent and child categories you could get the results you’re mentioning in the Real Estate category. The WP search function is not very good out of the box. You may want to search for some info on customizations, but it’s not the best search functionality out there.

Being able to post classifieds is a different situation. There are some themes created specifically for classifieds,, although the one that comes to mind first is no longer available because the developer seems to have disappeared.

donnie January 21st, 2009

Thanks for the code/script :)
useful

Yours sincerely,
donnie

Matt January 28th, 2009

very helpful thanks!

Will January 30th, 2009

Hi

Thanks a lot, the article was really helpful.

Good stuff, it was really helpful.

Thanks again, I appreciate it, sharing!

dread January 31st, 2009

nothing here that’s not already in the codex.

Vandelay Design February 1st, 2009

Dread,
Yes, I realize that. But sometimes the Codex is hard to use because there is so much info there. It can be helpful to have it broken down.

mick e February 1st, 2009

Hey – thanks for this article – it is a good’n. and the fist tip worked great. However, I would like to relay my little glitch experience. (warning I am a relative WP noob)

When I tried to do the “Exclude Posts in a Category from Appearing on the Homepage” on my site, the code somehow breaks the page chain, so when the user clicks on the “next” link (/?paged=2) it just redisplays the top posts again, and continues to do so after clicking again (/?paged=3) and so-on.

FWIW, the theme I am using is “MassiveNews” a magazine style format.

Vandelay Design February 1st, 2009

The fact that it’s a magazine theme probably has something to do with it. I recommend trying the advanced category excluder plugin if it’s not working the manual way.

mick e February 2nd, 2009

Well I tried the plug-in, and that introduced a whole new set of issues that I didn’t want to have, so I unplugged it and started over. I think there was a toxic cocktail of MassiveNews, EventCalendar and ACE going on. So I cranked up the iTunes and spent the evening sacrificing goats and waving chicken bones. Sometime around 1am I looked at the screen and everything was working.

I wish I could tell you what I did, but it was all a blur. Bottom line, I was able to use your code to make it work. Thanks bunches.

Richard February 10th, 2009

Very enlightening post… thank-you. Can you give a code for how to show a COMMENTS list (in a sidebar or page) only from specific categories? Been trying this and still can’t get it right on 2.7

Thanks.

Eliott February 11th, 2009

Hi there, nice tut and i was wondering if there is a small function to display only one category in the main page.

I tried with many plugs but no one works ok.

Vandelay Design February 11th, 2009

Eliott,
Did you try the Advanced Category Excluder plugin?

Richard February 12th, 2009

I have found this plugin http://wordpress.org/extend/plugins/post-lister/ that covers most options for displaying and selecting posts/comments from selected categories. I’ve tried it locally on 2.71 and it appears to work ok. Needs a little tweaking to get things right, but all the variables and functions are there. This works in a page/post as well as a widget.

albert March 7th, 2009

Thanks for the code , but check the link example don’t work.

“usefulhttp://example.com/category/nameofyourcategory/feed/”

João Dias March 16th, 2009

Great Tut men, finaly i can have my category especific pages, and all thanks to you.

As you can see, i coul get this to work in http://infpt.com, just choose “dicas” or “jogos” and you can see the pages working.

I just had to copy the page template and add the css tags to this code.

If you use it simple, it will show like a normal blog.

The code i used was:

<a href=”">

<a href=”">Read More

JECCI April 1st, 2009

“Exclude Posts in a Category from Appearing on the Homepage:” breaks all the links on the home page so we can use that… You should update your code.. and Ryan’s fix doesn’t seem to work either… I am still looking for a solution…

Vandelay Design April 2nd, 2009

JECCI,
Sorry. That may be related to changes in newer versions of WordPress. I’ll look into it when I have the chance.

jason April 4th, 2009

Hey thanks for the great tweaks.
One i found the correct pages in my template, i was able to make the correct changes.

I just love the Show Only Post Titles on Category Pages, its a great way to better organize categories so its more like a traditional non blog website.

Your tips are truly gems.

thanks again.

Wordpress Themes April 4th, 2009

Excellent tips. Wordpress is a great platform and to attempt to suit everyone would be a nightmare. Tips like these are what make Wordpress such an excellent CMS. Easy to customize and many good folk devoting quality time and energy to all facets of the Wordpress CMS.

mssmotorrd May 3rd, 2009

It’s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
p.s. You have a very good template for your blog. Where did you find it?

תוכנה לביטוח May 4th, 2009

Great article, thank you for sharing !

jd May 30th, 2009

Hi I hope you can help me with this category modify problem. I want the category to show just the titles – no content and I’ve tried deleting the code:

<?php the_content(‘Continue Reading »’);>

or replacing “content” with “title” but the content still shows.

Here are first couple of lines of the “archive” page:

<?php the_content(‘Continue Reading »’);>

Any advice would be appreciated. Thanks in advance.
jd

jd May 30th, 2009

ok…the form is not allow me to post code..

Vandelay Design May 30th, 2009

jd,
The PHP code that says the_content is what is pulling the posts and displaying them. Try removing that code. You want to leave the code above it that calls the post title and the link.

begamo June 1st, 2009

Nicely written, very easy to follow. Thanks.

Johnny June 5th, 2009

Hi,

At the bottom of every each post I got the link that go back the category. How do you remove it? Please help me out.
Thank you all.

Vandelay Design June 6th, 2009

Hi Johnny,
Without seeing your theme’s code I can’t really say for sure, but if you look in the single.php file below the loop you should see some PHP code that pulls the category. If you remove that it should work.

Brainerd Realtor June 15th, 2009

This is seriously one of the best posts I’ve ever read. You inspired me to clean up my categories and trust me, that wasn’t easy! This stuff is super important and it’s hard to find it all on one website/page like this. Thanks so much!

fikret June 17th, 2009

does anyone know how can i control number of posts in archive page or category page?:S

Vandelay Design June 17th, 2009

fikret,
The Custom Post Limits Plugin is one easy way to control the number of posts on those pages.

fikret June 17th, 2009

thx bro

webuildlink.com June 22nd, 2009

Thanks for your article. I need to know the current category id when a category is clicked , what functions/variable is available for finding the current category.

I have found some code like $cat=get_query_val(‘cat’);
but echo $cat is not showing anything.

Please help

Thanks in advance

jigolo June 30th, 2009

very nice , great article thank you.

Website Design Services July 7th, 2009

Another great article. It does show how easy it is to change Wordpress to your liking. You don’t even need much knowledge of PHP.

Kino Creative July 9th, 2009

Thanks for that, be aware though that using this code to exclude categories from the home page will break the navigation. After much searching I found the solution!

Dave July 19th, 2009

Thank for the code. I will try it. Nice template

Iniara July 21st, 2009

This code doesn’t work for me and I included the correct category id.

I’m trying to follow Ryan’s fix for the exclude category in homepage but I can’t seem to make it work, maybe I copied it wrong or something. When I click to see the previous entries, I get the same ones from the first page.

auz1111 July 23rd, 2009

Anyway to exclude categories fro mthe default WP calendar widget? I only want to show post dates from a specific category.

auz1111 July 23rd, 2009

This would make my day if possible!

Dmitriy July 30th, 2009

There is a new program that allows you to create and download WP themes directly from Photoshop (Divine plug-in). It will be interesting to both beginner and advansed users.

sms September 24th, 2009

can you please post that how can we include specific tag pages in category list ?

Hardeep Singh November 4th, 2009

Some of the readers faced the same problem as I did, related to removing categories from the category sidebar and calendar. Please refer to my blogpost here on how I fixed it:

http://blog.hardeep.name/computer/20090228/hiding-wordpress-categories/

Vertical jump reviews November 11th, 2009

Thanks for this awesome post. I needed to exclude certain categories from homepage and this worked really well

Billy January 1st, 2010

Is there a way to Exclude Specific Categories from a single post? say for example this post is in two categories WORDPRESS and TWEAKS. but I want the WORDPRESS category excluded, so it would look like this. Published September 14th, 2008 in TWEAKS. Thanks In Advance

Vandelay Website Design January 2nd, 2010

Billy,
I’m not aware of any way to do that.

Trackbacks

Leave a Reply