WordPress Theme Tutorial: Displaying Recent Articles, Excluding From Some Categories
By default, you can use wp_get_archives() to display a list of recent articles. Nevertheless, it doesn’t allow you to set which category to include/exclude from the list. Here’s a way to do that:
<ul>
<?php
// display recent posts excluding those from certain categories.
query_posts("showposts=10&cat=-4,-5,-6,-7,-8,-9"); // grab 10 posts, negative to exclude cat IDs
while (have_posts()) :
the_post();
?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
The key here is to tweak query_post’s parameter to your liking. “showpost” means how many post you want to display, while “include” can be used both to include or exclude a category. Note that here you used a category ID. Normal number means you include that category, while adding a negative (”-”) symbol before the number means that you exclude that category. You use comma (”,”) to separate multiple IDs.
So, my query_post above can be translated to “get a list of ten most recent posts, excluding those that belongs to category ID 4, 5, 6, 7, 8, 9″.
One Comment Add yours!
Thanks so much, you are awesome! You saved me so much time!!!