This code snippet will let you output posts from a specific category in WordPress. Add the following code to the top beginning of your WordPress loop, and change the number 4 to the category number you would like to output.
<?php query_posts('cat=4'); ?>
As the loop runs, it will only look at posts that belong to category 4 in your WordPress. There are, however, many other extensions you can use with this code.
One helpful feature is to exclude certain categories from being outputted onto your site. You can use a
- sign infront of the number, and this will tell WordPress to exclude it from the loop.
<?php query_posts('cat=-5'); ?>
In the above case, WordPress ignores any posts in the 5th category.
You can also use this feature to add multiple categories into your WordPress loop, as well as include a list of categories to exclude. Simply add a
, between the category numbers.
<?php query_posts('cat=4,-5,7,9,-10'); ?>
The above code will output all posts in categories 4, 7 and 9, while excluding all posts in category 5 and 10.
If you have any questions on how this works, please feel free to leave a comment below and I’ll answer it as soon as possible.