a minor technicality

neil dixon’s blog, journal, and list of stuff he does

RSS2.0 Feed

Tweaking Wordpress – excluding categories from the front page

Tweaking Wordpress – excluding categories from the front page

Why would you want to exclude posts of a particular category from your blog’s home page?

I have just set up my del.icio.us library account to automatically post my latest del.icio.us links to this blog. This gives me a place to find the most recent additions, and place for you to be nosey about what I’m looking at, liking, and saving for later (you sneak, you). But the last thing I want is a big list of links popping onto the home page every morning and getting in the way of the ‘real’ content – particularly irritating for you, dear blog reader, if I do not manage any other posts in a day. So, I have created a specific blog category for the del.icio.us listings, and have excluded any posts to that category from appearing on the home page. You can get to the del.icio.us category through the category links on the right hand sidebar (somewhere over there—===>>>).

There is a plug-in for removal of a category from the home page, but I could not get it to work properly. However, the code tweak to your template is minimal.

To exclude any posts from a particular category from appearing on your Wordpress blog’s home page, open the index.php file, which you will find in your blog’s theme folder: /wp-content/themes/whatever-the-theme-name-is/

Between the line:

if (have_posts()) {

and

while (have_posts()) {

Drop in the following code:

if (is_home()) {
        query_posts('cat=-xxx');
  }

You must replace ‘xxx’ with the category ID which you want to exclude – get that from your wp-admin > Manage > Categories page. Note the minus (-) sign before the category ID, that is important, so make sure it retained.

Multiple category exclusion

To exclude more than one category, it is just a matter of listing each category ID in query_posts(), thus:

query_posts('cat=-12,-14,-22');

I am doing a great deal of behind the scenes tweaking on this blog both for enhancing life for you and I, and boosting things such as SEO (Search Engine Optimisation), so expect more posts like this one as I want to share this info.

A note about del-icio.us

The reason I’m using del.icio.us a lot more now – and it’s particularly useful recording research for TableRappers – is by fully integrating it with my Firefox browser. The new del.icio.us add-on, offers true desktop integration of del.icio.us bookmarks in a way that other desktop clients I have tried have failed terribly. Works for me, works very well.

8 Responses to “Tweaking Wordpress – excluding categories from the front page”

  1. This is very handy. Just what I need at the moment for a section of the site I want to show up separately from the main SmallBizPod blog feed.

    Does this piece of code exclude the category from the RSS feed as well? Or is it just simply a way of making sure a category doesn’t get displayed on the homepage?

  2. neil says:

    Good question. This will only work for the actual home page as yu are editing the blog’s default home page file. I’ll do some digging on the excluding from the feed and post here is I find anything.

  3. neil says:

    There’s a tutorial for excluding category posts from your RSS feed here:
    http://zeo.unic.net.my/notes/exclude-category-in-wordpress/

    It means adding to (or creating) the functions.php file in your theme folder, then placing a filter into it. Seemed to work for me here, though watch out for changes appearing not to take immediate effect due to the feed being cached.

  4. Shaun says:

    Excellent post! This is exactly what I was looking for, also. I’m actually not going to implement it just yet, but I know there’ll come a time when I want to remove my more trashy blog entries from the homepage.

  5. mp3bag says:

    Thanks, tweaked my site :) Working flawlessly

  6. Emma says:

    How would I get the category that I exclude to show up on another page? I have a category that I only want to appear on a certain page, not the homepage. :-)

  7. neil says:

    @Emma: you would need to add code to determine whether the page being viewed was the home page or not. See the is_home() function call in the Wordpress codex for more info on how to execute code only on the home page.

  8. David says:

    Hey, this worked great.

    One little thing that happened to me, not sure exactly why it happened, but pagination stopped working. What appeared on the front page would be the same as page 2 and so on.

    I made a small change that seems to have fixed it.

    query_posts($query_string . “&cat-xxx”);

    I just changed your line of with query_posts to this and it worked. :)

© NeilDixon 2006-2010. All rights reserved. MyFreeCopyright.com Registered & Protected