Display posts from specific categories on a Page
November 8, 2007 – 12:09 pm | by Miriam SchwabYou may want to display posts from a specific category on the homepage or other pages of your site. For example, you might want to have a box that displays the latest three posts from your News category, or have links to your latest podcasts.
In a previous post, I gave the code that would display the latest posts from a specific category in the sidebar. However, that code will not work in the main content part of the page. In order to display posts from a specific category in the main content section of a page, you need to use a variation on the WordPress Loop, which is as follows:
<?php query_posts('category_name=special_cat&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile;?>
To use this code, change the category name to whatever the name is of the relevant category, and change the number of posts displayed from 10 to whatever number you want. Where it says <!-- Do special_cat stuff... -->, put in all the code you usually use to display the title, date, category, etc. - whatever you need.



9 Responses to “Display posts from specific categories on a Page”
By mccormicky on Dec 14, 2007 | Reply
I’m a little confused.You don’t mention in this article making a separate template file to insert this modified loop into so I am assuming that I’d add this TO the loop?
Or if I DID use my own template file which is basically just my page.php file copied could this code work Exactly as you have written it?
Does categry_name refer to the category slug?Or do I use the id?
I’ve messed around with this but so far all I get is a blank page with my title showing.
By Miriam on Dec 14, 2007 | Reply
mccormicky - you have to use this instead of the loop in the page where you want to display posts in a specific category. You probably don’t want to do this on every page of your blog, so you have to figure out where you want the blog posts from one category to appear, and then you replace the loop on that page.
For example, if you have a unique design for your Home page, and you’ve created a page template called home.php, you replace your loop in that template file with the code above.
The easiest way to do it is simply to replace the first line of the regular loop, which is something like < ?php if (have_posts()) : ?>, and replace it with < ?php query_posts('category_name=special_cat&showposts=10');?>
Where it says “special_cat”, you replace that with the NAME of your category, which is the slug.
By mgmt on Mar 5, 2008 | Reply
I think if you replace this line of code <?php query_posts(’category_name=special_cat&showposts=10′); ?>
with this line <?php query_posts(’cat=2&showposts=10′); ?> it will be easer to understand
all you have to add to this is that 2 is the ID of the category you want to show
By Miriam Schwab on Mar 9, 2008 | Reply
Hi mgmt - your way works well too. It’s just a matter of either specifying the category slug or ID. Some may find one way easier, some the other, and some may not have a preference one way or the other.
By Sean on Jun 6, 2008 | Reply
Hi there, I’m having a bit of a problem with this technique, as soon as I add that new line of code, and I have a <?php else : ?> following the endwhile for a ‘Not Found’ statement, I get this error:
Parse error: parse error, unexpected T_ENDWHILE in D:\hshome\armadaad\armadadesign.ca\conversation\wp-content\themes\armada\events.php on line 29
Any thoughts?
By Sean on Jun 6, 2008 | Reply
My apologies, the error actually was:
Parse error: parse error, unexpected T_ELSE in D:\hshome\armadaad\armadadesign.ca\conversation\wp-content\themes\armada\events.php on line 31
By Ryan on Jun 9, 2008 | Reply
Sean - You will need to post your PHP here for us to figure out what the problem is. That error message is too general to bugfix from it.
By Andy Howell on Jun 13, 2008 | Reply
Handy little piece of code