Display posts from specific categories on a Page
November 8, 2007 – 12:09 pm | byYou 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.



60 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
By Kim Woodbridge on Sep 13, 2008 | Reply
Thanks for the tip! I’ve been working with this today and couldn’t get it to work until you explained it so simply.
By Atle on Oct 19, 2008 | Reply
Thank’s for one great blogpst
By Atle on Oct 19, 2008 | Reply
And I forgot to mention that this solution does not work with me when I used the category name…
I had to take advantage of category ID to make everything work! Anyway, thanks a lot!
By ansel on Nov 14, 2008 | Reply
Thanks so much! This bit of code was crucial in work on one of my sites.
By Perry on Dec 2, 2008 | Reply
im having troubles with this displaying. where do i put it in this code of my template code iv been trying for hours…
<?php
/*
Template Name: Archives
*/
?>
<?php get_header(); ?>
<div id=”post”>
<div class=”post-meta” id=”post-<?php the_ID(); ?>”>
<h1 class=”post-title”><?php the_title(); ?></h1>
<div class=”postedby”>
<div class=”post-status”>
<strong><?php _e(‘Posted by’); ?></strong> <?php the_author_posts_link(); ?> in <?php the_time(‘F jS, Y’) ?> <?php edit_post_link(‘Edit’, ”, ”); ?>
</div>
</div>
<div class=”clear-fix”></div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
By Christo on Jan 23, 2009 | Reply
wow thanks mate, this was really helpful.
By Christo on Jan 23, 2009 | Reply
If I may be so bold, try putting the loop in front of your code like so:
START QUERY
POST
END QUERY
your not calling the loop, so it won’t work, try this:
<!– query needs to be called to display your project category… –>
<?php query_posts(‘category_name=projects_cat&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– the post itself… –>
<div id=”post”>
<!– post formatting goes here .. –>
</div>
<!– end it… –>
<?php endwhile;?>
By prez on Jan 29, 2009 | Reply
where do u find the templateyou have to edit?
By dash on Feb 6, 2009 | Reply
Hi!! exactly what i am trying to do! I want to display all posts from a category (photoshop) in a page; but.. where do i put your phph code?? thank you inadvance!
By Ryan on Feb 8, 2009 | Reply
@dash. We can’t help you with that. You need to decided where to put it yourself!
Without seeing your theme files and knowing where on your page you want it to show there is no way anyone can help.
By amanda on Feb 16, 2009 | Reply
Just what I needed, thank you so much!
By vinoth on Feb 23, 2009 | Reply
Here you have given to display the categories. it is a nice one. but what i am asking is how to display the excrepts of the categories we put on this code…
<?php query_posts('category_name=special_cat&showposts=10'); ?><?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile;?>
if change the_post(); to the_excrept(); it won't work.. help me please
By Larry Long on Mar 4, 2009 | Reply
When I add this code I can’t password protect the page. I want to add a page that is password protected buy only displays posts from a specific category. Any tips on how I could do that?
By Arjan D. on Mar 27, 2009 | Reply
First off, thanks for this really useful tip!
I have a question though.. How can I make the specified category dynamic. Instead of a static category I want to make it like that, so it chooses the title of the page as a category. Maybe my explanation isn’t so clear so here’s an example.
I make a new page. It’s called “Juniors“.
<?php query_posts('category_name=PAGENAME&showposts=10'); ?><?php while (have_posts()) : the_post(); ?>
<!-- Do special_cat stuff... -->
<?php endwhile;?>
Where it says “PAGENAME” in the code, I want to let the “Juniors” show up automatically. Is this possible?
Thanks for sharing knowledge.
Best regards
By Kenneth Watt on Apr 7, 2009 | Reply
Sorry mate – I think that my problem might be caused by what you are saying here. I want to have multiple ones of this here, but it doesn’t seem to want to change the category for the second one.
Email me back if you want: kennethwatt(at)technofinger[dot]com
By zack on Apr 29, 2009 | Reply
does this work when creating a new page from wordpress or do you have to create a new php file to make this work?
By Mike on May 1, 2009 | Reply
This worked great for me.
How should I go about sorting them? If I want them to be sorted alphabetically, and not by post date.
I tried &sort_by=title, but that didn’t work.
By Mike on May 1, 2009 | Reply
Scratch that comment. I figured it out. &order=ASC or &order=DESC
By Roberto Breve on May 6, 2009 | Reply
You can also do this if you want to display posts from a specific category on a specific page
<?php
if ($page_id==11){
query_posts(‘cat=1&showposts=10′);
};
if($page_id==8){
query_posts(‘cat=3&showposts=10′);
}
?>
By Mark on May 18, 2009 | Reply
I have a question. How do you have a little box on your site showing say the first 150 words of the latest article of a category other than the one you are displaying on the main page?
By Fission on Jun 10, 2009 | Reply
This was such a big help! I’ve been looking over the difficult-to-traverse WordPress Codex for a while looking for how to do this!
By Grif on Jun 10, 2009 | Reply
Hi, thanks for this, however i can’t seem to get it to work.
Here is the template page code i’m using.
<?php
/*
Template Name: for-sale-template
*/
get_header(); ?>
<div id=”content” class=”narrowcolumn”>
<?php query_posts(‘category_name=homepage&showposts=10′); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<h2><?php the_title(); ?></h2>
<div class=”entry”>
<?php the_content(‘<p class=”serif”>Read the rest of this page »</p>’); ?>
<?php wp_link_pages(array(‘before’ => ‘<p><strong>Pages:</strong> ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link(‘Edit this entry.’, ‘<p>’, ‘</p>’); ?>
</div>
<?php //get_sidebar(); ?>
<?php get_footer(); ?>
Nothing shows on the page even though there are posts in the category called “homepage”.
Any help appreciated!!!
By Grif on Jun 10, 2009 | Reply
OK, found the issue. A plugin called: AStickyPostOrderER
By Grif on Jun 10, 2009 | Reply
one other thing, what if i want the normal loop to display also? Is it bad to have 2 loops?
By Rob on Jun 17, 2009 | Reply
Thank you.
By Bhaskar on Jul 2, 2009 | Reply
I want to display those posts which do not have any category.Can u help me out?
By PeterKohl on Jul 5, 2009 | Reply
@Roberto Breve – Thanks …that is by far the easiest I’ve seen as it allows you to only use one .php page yet have multiple WordPress Pages display separate categories…
Quote:
<?php
if ($page_id==11){
query_posts(’cat=1&showposts=10?);
};
if($page_id==8){
query_posts(’cat=3&showposts=10?);
}
?>
By b00m on Jul 6, 2009 | Reply
The code is working…Tnx!
By Daniel Fischer on Jul 16, 2009 | Reply
Thank you for this article and tip. I am having trouble getting it to work. I entered the following into the HTML portion of a wp page:
<?php query_posts(‘category_name=COLUMNS&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<!– Do special_cat stuff… –>
<?php endwhile;?>
What is supposed to go in the place of “<!– Do special_cat stuff… –>”
I’m a bit confused on this part of it.
Please advise.
Thank you so much for your help!
Daniel
By Louis-Philippe Dea on Aug 21, 2009 | Reply
Hi guys, first thank you for all those tips!
I have a little question, how do I remove one categorie from the main page containing all my posts. For example, I’ve made one page containing posts from only 1 category and I would liketo remove this category from my main page.
Thank you guys for your time!
By Louis-Philippe Dea on Aug 21, 2009 | Reply
Nevermind, I just saw the link at the bottom of my post… so stupid
By chuck on Aug 26, 2009 | Reply
Is it possible to display posts by AUTHORS on a page using same script? What would the query_posts line look like?
Thanks so much!
By jay on Sep 7, 2009 | Reply
Hi, yeah its a handy code and will be using this version.
But say if i wanted to go one step further and show not just ONE but THREE categories only on a page?
By iFadey on Sep 21, 2009 | Reply
It really solved my problem. Thanks
By Doobie on Oct 4, 2009 | Reply
I noticed that Pagination doesn’t work with this technique… Any clues?
If I show 1 post per category, the previous posts link opens a page with the same 1 post. Thoughts?
Thanks.
By Cliff on Oct 10, 2009 | Reply
When I enter the suggested code in a new page template file, it works but also removes display of important items like the Page Title and any content that is entered into the Page body.
In other words, when using this solution I can ONLY show Posts from a selected category on the Page and nothing else.
For example, I’d like to have a page maintains the Page Title plus some introductory text and then below the display of the category Posts.
Here’s the code I’m using (sorry no live version to show yet):
<?php query_posts(‘category_name=shops&showposts=1000&orderby=title&order=asc’); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
By Aldo on Oct 12, 2009 | Reply
Worked perfect.
By English Anime on Nov 2, 2009 | Reply
I want to have the posts appear on the homepage, with a page of text above the posts. Is that possible using this code?
By syncmaster on Nov 3, 2009 | Reply
@Cliff:
Try adding this before your code:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
(post stuff here)
<?php endwhile; endif; ?>
By Gavin Steele on Nov 3, 2009 | Reply
I have a loop that i use to display a carousel at the top of my site. Currently it displays all entries, but I want to use your code to restrict the outputted images and info to only posts in a specific category.
Where would I add you code into this loop?
<?php
$dock = new WP_Query();
$dock->query( ’showposts=8′ );
while( $dock->have_posts() ) : $dock->the_post();
?>
By WordpressThemes on Nov 8, 2009 | Reply
Im going to use it in my blog to display “daily tips” in sidebar.Great piece of code.
By mark on Nov 9, 2009 | Reply
Hi,
Yes, good code… thank you everyone.
I created a new template and page… using the above code, I was able to display every thing from category “polls” on my new “polls template”.
However, is there a way to exclude category “polls” from my main template?
Thank you,
By Quinton on Nov 9, 2009 | Reply
It would really help a lot for us newbies if someone can tell us where to use it in the code.
By Horst on Nov 9, 2009 | Reply
@Louis – how did you solve the problem ” how do I remove one categorie from the main page containing all my posts”?
It is the same to me. Unfortunately I’m not a programmer.
By mcloud on Dec 31, 2009 | Reply
Is any of above codes works on HTML pages???
Thanks
By Ivy on Jan 5, 2010 | Reply
You’re brilliant! Thank you!
By Chris on Jan 13, 2010 | Reply
Hello, I have gotten this to work but any content that I have on my page gets attached to each individual posting. How can I stop this? Ive tried rearranging the code everywhere but it just does not work. Is there a break code or something??? Here is my code so far:
<?php /* Template Name: News
*/ ?>
<?php global $themeoptionsprefix; get_header(); ?>
<?php query_posts(‘category_name=featured category #1&showposts=10′); ?>
<?php while (have_posts()) : the_post(); ?>
<?php endwhile;?>
<div id=”content”>
<?php if(get_option($themeoptionsprefix.’_sidebarpos’) == 1){ get_sidebar(); } ?>
<?php if(get_option($themeoptionsprefix.’_sidebarpos’) != 2 || get_option($themeoptionsprefix.’_sidebarpos’) != 1 ) { get_sidebar(); } ?>
<div class=”postarea”>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<br />
<img src=”http://i813.photobucket.com/albums/zz57/swag411/logo3.jpg” border=”0″ alt=”Photobucket”></a>
<br />
<?php $readrest=__(“Read the rest of this page”,’NewsMagazineTheme640′); $edent=__(“Edit this entry”,’NewsMagazineTheme640′); $pgswrd=__(“Pages”,’NewsMagazineTheme640′); ?>
<div class=”entry”>
<?php the_content(‘<p class=”serif”>$readrest »</p>’); ?>
<div class=”postareameta”><?php edit_post_link($edent, ‘<p>’, ‘</p>’); ?></div>
<?php wp_link_pages(array(‘before’ => ‘<p><strong>$pgswrd:</strong> ‘, ‘after’ => ‘</p>’, ‘next_or_number’ => ‘number’)); ?>
</div>
<?php
$commentsonpage=get_option($themeoptionsprefix.’_commentsonpage’);
if(!isset($commentsonpage) || empty($commentsonpage) || ($commentsonpage == ‘yes’) )
{ ?>
<?php if ( comments_open() ) : ?>
<div class=”commentarea”>
<?php comments_template(); // Get wp-comments.php template ?>
</div>
<?php endif; ?>
<?php } ?>
<?php endwhile; endif; ?>
</div><!–end postarea–>
<?php include (TEMPLATEPATH . ‘/includes/sidebar2.php’); ?>
<?php if(get_option($themeoptionsprefix.’_sidebarpos’) == 2) { get_sidebar(); } ?>
</div><!–end content–>
<?php get_footer(); ?>
You can check out my website at Swag411.com and click on the news tab to see what I am talking about. Also, how do I give the posts a date, author, and frame? Thanks in advance for the help!
By Ethan on Feb 5, 2010 | Reply
I’m having the same problem as Doobie above – the query works great, but pagination doesn’t. In fact, I’m not getting a “previous posts” link at all.
Ideas??
By dandy on Feb 9, 2010 | Reply
I am having the weirdest issue in my entire life. The above code displays results in firefox but not on anyother browser…. this is insane.. its not css/html issue. I’ve looked at the source of page in all browsers. the posts are fetched only in firefox. here is my link. You can check for “Client testimonials”. Only displays in firefox.
http://webdevelopment.com.sg/dev/allied/
Any help would be appreciated.
By Livio on Mar 9, 2010 | Reply
check out this plugin:
http://wordpress.org/extend/plugins/page2cat/