Vandelay Website Design Blog

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

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

Originally Published September 14th, 2008

77 Responses

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

  2. Vandelay Design
    September 14th, 2008

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

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

  4. NaldzGraphics
    September 15th, 2008

    thanks for sharing. is very useful

  5. 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!

  6. Dainis Graveris
    September 15th, 2008

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

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

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

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

  10. Vandelay Design
    September 15th, 2008

    Jamie,
    Thanks for sharing.

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

  11. Seth Holladay » Links » links for 2008-09-16
    September 16th, 2008

    [...] Category Hacks for WordPress Theme Designers | Vandelay Website Design (tags: wordpress) [...]

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

  13. WordPress, come escludere determinate categorie in svariate situazioni - Geekissimo « R&M Project
    September 18th, 2008

    [...] Via | Vandelay Design [...]

  14. Leonaut.com
    September 18th, 2008

    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…

  15. The Best of the Blogosphere: September 23, 2008 | Super Blogging
    September 23rd, 2008

    [...] Category Hacks for Wordpress Theme Designers at Vandelay Design. [...]

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

  17. My Monthly Favorites: September 2008 | Naldz Graphics
    October 1st, 2008

    [...] Category Hacks for WordPress Theme Designers (VandelayDesign) [...]

  18. A Playoff Worthy Lineup Of Links - This Month In SEO - 9/08 | TheVanBlog | Van SEO Design
    October 1st, 2008

    [...] Category Hacks for WordPress Theme Designers [...]

  19. 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?

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

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

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

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

  24. Top 10 Tutorials for Developing WordPress Themes | Vandelay Website Design
    October 22nd, 2008

    [...] Category Hacks for WordPress Theme Designers [...]

  25. Top 10 Tutorials for Developing WordPress Themes
    November 23rd, 2008

    [...] Category Hacks for WordPress Theme Designers [...]

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

  27. 2008 Most Popular Design posts, Tutorials and Resources
    December 21st, 2008

    [...] 46. Category Hacks for WordPress Theme Designers [...]

  28. 2008 Most Popular Design posts, Tutorials and Resources | Web Hosting and Domains
    December 31st, 2008

    [...] 46. Category Hacks for WordPress Theme Designers [...]

  29. 2008 Most Popular Design posts, Tutorials and Resources | SulVision
    December 31st, 2008

    [...] 46. Category Hacks for WordPress Theme Designers [...]

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

  31. 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=”">

  32. 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
    :-?

  33. Excluding a particular category posts from home page | Sriraj.org
    January 12th, 2009

    [...] plugin existed for this.I didn’t find a plugin but I certainly did find a method to do this. Vandaley posted few category related hacks which I thought I might use . But there was a trouble in using [...]

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

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

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

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

  38. AMB Album » 2008 Most Popular Design posts, Tutorials and Resources
    January 15th, 2009

    [...] 46. Category Hacks for WordPress Theme Designers [...]

  39. 2008 Most Popular Design posts, Tutorials and Resources | Web2.0
    January 19th, 2009

    [...] 46. Category Hacks for WordPress Theme Designers [...]

  40. donnie
    January 21st, 2009

    Thanks for the code/script :)
    useful

    Yours sincerely,
    donnie

  41. Matt
    January 28th, 2009

    very helpful thanks!

  42. How to Easily Customize the WordPress Image Caption
    January 29th, 2009

    [...] Category Hacks for WordPress Theme Designers (Vandelay Design Blog) [...]

  43. 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!

  44. dread
    January 31st, 2009

    nothing here that’s not already in the codex.

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

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

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

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

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

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

  51. Vandelay Design
    February 11th, 2009

    Eliott,
    Did you try the Advanced Category Excluder plugin?

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

  53. albert
    March 7th, 2009

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

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

  54. doycetesterman.com | Just some notes to myself on things to do with the wordpress code.
    March 10th, 2009

    [...] Category Hacks for WordPress Theme Designers [...]

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

  56. 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…

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

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

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

  60. Erica’s Themes
    April 21st, 2009

    [...]   http://vandelaydesign.com/blog/wordpress/category-hacks/ [...]

  61. How to Disassemble a Free WordPress Theme Part 1 | Designurimagination Blog - Let Your Imagination Fly
    April 23rd, 2009

    [...] function and other cool things you can do with it, check out Vandelay’s article on Category Hacks for Wordpress Theme Designers and the trusty Wordpress [...]

  62. 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?

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

    Great article, thank you for sharing !

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

  65. jd
    May 30th, 2009

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

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

  67. begamo
    June 1st, 2009

    Nicely written, very easy to follow. Thanks.

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

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

  70. Avatars in Blog Comment Design | Vandelay Design Blog
    June 10th, 2009

    [...] Category Hacks for WordPress Theme Designers [...]

  71. 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!

  72. fikret
    June 17th, 2009

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

  73. Vandelay Design
    June 17th, 2009

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

  74. fikret
    June 17th, 2009

    thx bro

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

  76. How to Disassemble a Free WordPress Theme Part 1 | Quest For News, A TUTORIAL Base
    June 27th, 2009

    [...] function and other cool things you can do with it, check out Vandelay’s article on Category Hacks for Wordpress Theme Designers and the trusty Wordpress [...]

  77. jigolo
    June 30th, 2009

    very nice , great article thank you.

Leave a Reply