23 May 2007

I sometimes have to do certain things over and over again in Wordpress, like excluding certain categories from being shown on a page. Wouldn’t you know, I always forget how to do this. I figured the best way to keep track would to create a blog entry about it.

Below is the code that can be used to display/exclude certain categories on particular pages.

In the “Main Index Template” or any page you have a post query/Wordpress Loop look for and modify the following code:

  1. query_posts('cat=3,4,5,6&posts_per_page=2'); ?>
  2. < ?php if (have_posts()) : ?>
  3. < ?php while (have_posts()) : the_post(); ?>

To further explain for folks who are new to this:
In line #1 you’ll see some parameters being called. For example, “cat” stands for categories and you’re telling Wordpress that you only want to show categories 3, 4, 5, & 6 on the front page with a maximum of 2 posts. You can also call all categories and exclude just one using the following:

  1. query_posts('cat=-6&posts_per_page=2'); ?>

Or you can show only one category with the following:

  1. query_posts('cat=6&posts_per_page=2'); ?>

You can use this code on any page that uses the Wordpress Loop. To learn more about queries, visit the following Wordpress page on Query Posts.

1 Comment

  1. No. 1 Dave Bowker 05/24/07

    Nice. I’ve actually been doing this at the moment, but I’ve also been showing different categories to different pages. It can be quite time consuming to write in if there are alot of categories, but I guess if that’s what the client wants then that’s what the client wants.

    Here’s my code for example…


    Cat 1&orderby=id&use_desc_for_title=0&child_of=5');
    wp_list_categories('title_li=Cat 2&orderby=id&use_desc_for_title=0&child_of=9');
    wp_list_categories('title_li=Cat 3&orderby=id&&use_desc_for_title=0&child_of=13');
    } elseif (is_page()) { // if they are pages
    if (is_page('products')) { // which page they are, example products page
    wp_list_categories('title_li=Cat 1&orderby=id&use_desc_for_title=0&child_of=5');
    wp_list_categories('title_li=Cat 2&orderby=id&use_desc_for_title=0&child_of=9');
    wp_list_categories('title_li=Cat 3&orderby=id&use_desc_for_title=0&child_of=13');
    } elseif (is_page('about-us')) { // page is about-us
    echo "Show anything you like here";
    } else { // catches other 'pages' pages
    sideblog('title=true&permalinks=true&limit=2');
    }
    } else { // catches for all other pages
    sideblog('title=true&permalinks=true&limit=2');
    }
    ?>

Tell Me What You Think

About Me

My name’s Jen; I’m a cynical, sarcastic, ex-drummer who is fond of dark humor. I've held way too many factory & retail jobs but finally found my calling one Christmas holiday in a dark, musty basement. I am now a CSS & XHTML web standards looney and can be found daily—when I’m not at my Mac—at the local fair-trade coffee shop buying an iced-soy mocha no matter the temperature.

I am also the owner of Pop Stalin Design specializing in CSS & XHTML web design as well as custom WordPress themes.