Customizing WordPress: Limiting Categories on Your Home Page and Sidebar

Update: I’ve redesigned my site since writing this post. The advice in this post is still valid, but the descriptions no longer match what you see on my site.

If you click around into different parts of my site, you’ll notice that only certain categories of posts appear on the home page, and there are two variations to my sidebar. What I’ve done is make a single blog look like two different blogs: one for my professional work, and one for my personal posts. I’ve achieved these effects without using plugins, and without registering multiple sidebars. The only downside to my approach is that it’s not a technique you can use to control which widgets appear on which pages (but it’s fine if you want your widgets on all your pages). I don’t use widgets so it’s not a downside for me 😉 .

To follow along, you’ll need to be comfortable with editing a couple of your theme’s files, and be comfortable with the basics of PHP.

Limiting the categories on your home page can be done by adding a few lines of code to the top of your theme’s index.php file.

<?php if (is_home()) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts("cat=12,23,6&paged=$paged");
} ?>

For “cat=” on the 3rd line, list the numbers for the categories you want to appear on your home page (or you can put a minus sign in front of the category numbers to exclude specific categories). The WordPress documentation for query_posts says you can do this with simpler syntax, but you can’t. The documentation is wrong. If you follow the suggestion there, your “Newer Entries” and “Older Entries” links on your home page won’t work. They will just continually re-load the most recent posts. The code I’ve given here is explained nicely in this WordPress support forum post.

So with this code, my home page includes only the categories related to my professional work. For my personal blog, I needed a way to get posts from all the right categories to appear together on a page. The answer is simple: I created a single parent category that encompasses all the categories I use for my personal posts. So the page for that parent category serves as the “home page” for my personal blog.

I also wanted my sidebar to change depending on whether you are viewing the “professional” pages or the “personal” pages. Achieving this involves two steps. First, add the following code to the top of your sidebar.php file. The category and page numbers you see listed are the ones where I want the “professional” sidebar to appear, which I call $home_sidebar (so you should substitute category and page numbers appropriate for your site). Note the ability to pass an array to is_category is new with WordPress 2.5.

<?php
$home_sidebar = false;
if (is_category(array(12,23,6))
  || in_category('12') || in_category('23') || in_category('6')
  || is_page(array(345,440,496,501)) || is_home()) {
      $home_sidebar = true;
} 
?>

The second step is to check the value of $home_sidebar whenever you want to vary what appears in the sidebar. For example, to vary the category links in your sidebar, use code similar to this (again, substituting your category numbers). You’ll want to explore the options for the wp_list_categories function to get the exact appearance you want:

<ul>
<?php if ($home_sidebar) {
    wp_list_categories('include=12,23,6&feed=RSS&order=desc&title_li=');
}
     
else {
    wp_list_categories('child_of=26&feed=RSS&title_li=');
} ?>
</ul>

Note my use of child_of. This is the number for the parent category I created for all of my personal posts. This is a very convenient way to get all of the child categories without having to list them individually.

I’ve used this approach to make my blog look like two blogs. But you could easily extend this approach to subdivide your blog into as many mini-blogs as you like.

3 Comments

  1. Reply
    cleona May 5, 2008

    Hello Michael
    I have been looking for a way to get all my posts regardless of the date posted to appear on one page. For example if a person clicks on a category called “kitchen” then it should link to a page with kitchen related posts regardless of the dates i posted them. Is there a code for that?
    Thanks in advance

    ____________________________________________

    Cleona Vassell
    Real legitimate work at home job leads, forums, chat, and newsletter.
    http://www.workathomecooperative.com

  2. Reply
    Mike May 5, 2008

    Hi Cleona – do you mean you want all of the “kitchen” posts to show up on one page, no matter how many there are? AFAIK, the number of posts on a page will always match your setting for “blog pages show at most [X] posts” in the WordPress admin. You can use the query_posts function to override that, as explained here (see example 3 – I think that’s what you’re looking for).

  3. Reply
    Cleona July 5, 2008

    Hello Mike
    Sorry for taking so long to respond. Yes, you are correct – ““kitchen” posts to show up on one page, no matter how many there are?”
    Thanks. I will look into what you recommend.

Leave a Reply to cleonaCancel reply