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 2 3 4 5 | <?php query_posts('cat=3,4,5,6&posts_per_page=2'); ?> <?php if (have_posts()) : ?> <?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 2 3 | <?php query_posts('cat=-6&posts_per_page=2'); ?> |
Or you can show only one category with the following:
<?php 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.
One Measly Opinion
Dave Bowker | May 24
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');
}
?>