Display posts from specific categories on a Page
You 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.
Category: Code Snippets, WordPress as CMS










Im going to use it in my blog to display “daily tips” in sidebar.Great piece of code.
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,
It would really help a lot for us newbies if someone can tell us where to use it in the code.
@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.
Is any of above codes works on HTML pages???
Thanks
You’re brilliant! Thank you!
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!
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??
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.
check out this plugin:
http://wordpress.org/extend/plugins/page2cat/
many thanks dude… wonderful article. Just finished my custom blog with your help!
You’re welcome dude, but we’re not dudes here – we’re dudettes!
I hope someone still checks this posts. I read all the posts, but I’m a newbie here. I am using the Basic Simplicity theme and I know nothing about loops or anything. I want to display the 3 latest posts of a category in the home page, but only the excerpts.
Can someone please help me?
Thanks
First of all, thanks for sharing this. It seems to be working great. One thing I noticed was that anything else that came after this in my template would fail to work (such as a series of ifelse items I had in my sidebar). But, I found a fix, which is to make sure the query gets reset when you’re done with it.
So if you’re having the same problem as I did, add a reset at the end:
<?php endwhile;?>
<? wp_reset_query(); ?>
I love you! Thanks so much
Thanks for posting that fix MoDad!
You might want to add “wp_reset_query();” after the endwhile to not possibly mess up other parts of the page. Took me half a day to figure this out. Seems to work now though… thanks.
Sory to disturb,
I do not understand where to put the code….
But
I want to display post through pages . How can i . can you help me out.
I want to display post on this site
http://www.islamabadid.com
Thanks
Can we get a working example of this code please? I don’t know what code to put in to display the title, date, category, etc.
Thanks
Hi, it seems like <?php while ( have_posts() ) : the_post(); ?> this loop is not happening in my website..but I do have posts.. do you know what’s happening?
Thanks hey! helped a tonne!