WordPress: display posts from certain category
Recently my client’s design required displaying posts in sidebar only from one category. To solve this problem I decided to use class WP_Query.
The first we have to create instance of WP_Query:
Then call method query(); to start the query. This is actually same as you would be using query_posts();
Parameter cat indicates which category’s posts are shown. Parameter showposts is the number of posts to be displayed.
query('cat=1&showposts=5'); ?>Now we can use loop:
<?php while($recent->have_posts()) : $recent->the_post(); ?> <!-- Here goes some code --> <?php endwhile; ?>
Final code that displays last 5 posts’ permalinks from category with ID=1 looks like this:
<h2>Last posts</h2>
<?php $recent = new WP_Query(); ?>
<?php $recent->query('cat=1&showposts=5'); ?>
<?php while($recent->have_posts()) : $recent->the_post(); ?>
<ul>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
</ul>
<?php endwhile; ?>
Using your own WP_Query instance and loop prevents you from problems that might cause plugins’ loops and use of conditional tags.





Thanks a lot… I was looking for this very simple solution for 3 hours… Thanks again.
btw do you know how I can stop showing posts from certain category? Because I only need them for my menu
@MacTeP
You can stop showing posts from certain category by adding minus before value of the category.
This will stop showing posts from category with ID 1:
< ?php $recent->query(‘cat=-1?); ?>
thank you so much, this was an incredible help after breaking my loop 324234 times with hacks
Last posts
<a href="”>
Is there a way to have the picture in post show up also ?
thanks for this – such a simple solution – and yet took me ages to find it on the net. i tip my hat to thee fine fello
Brilliant mate! You’ve been a great help, thank you!
Hey
Great code, works like a charm. Super!
I was wondering though, is there a way to get current-cat on the li’s?
Thanks!
You can check how to adjust category items here http://codex.wordpress.org/Template_Tags/wp_list_categories
Thats was good. I was really messed up. Thank you
Cheers for the code snippet. Helped me out on my latest project.
Amazing stuff Tom!! Thank you for sharing this.
Just as matter of interest, I was wondering if there is a way to parse through the style of a certain post when fetching the content with your solution.
Thanks this is what I needed. Trying to show some related posts on my sidebar..
right now i am displaying posts like. when user click on any post it is showing on top and remaining 4 posts displaying below. and when i click on downside post it supposed to come on top and rest of posts needs to follow downside.
Worked great man thanks a lot! Spent endless hours looking for the right plugin.
Thank you for making sense of this, I have been trying loads of different methods to have multiple loops on a page in wordpress but was getting confused by other peoples’ explanations.
Yours lays things out plain and simple and the code worked straight away.
Thank you for taking the time to share your knowledge of WP.
Thanks this is what I needed.
Wow cool hack… My blog looks cool with this widget
Hi,
Thanks I was really stuck up. But you solved my problem. But one more trouble.
How can we use content=no excerpt=no and ascending and descending order
Thanks for the help. I needed to keep using things from the original post query so once I was done with the “$recent” object I had to set up the original again by doing this:
have_posts();
the_post();
Doesn’t work with 3.0. cat=1 cat=5 etc…. all show the last posts from your BLOG not from any specific category
I put this in the loop, right?….
<h3>Last posts</h3> <?php $recent = new WP_Query(); ?> <?php $recent->query('cat=1&showposts=5'); ?> <?php while($recent->have_posts()) : $recent->the_post(); ?> <ul> <li> <a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> </ul> <?php endwhile; ?>Any help please?
no you have to put it outside of loop
Tried it everywhere and still only shows the last posts of the entire blog.
If I use cat=1 it shows the last posts of the blog but cat=2 cat=3+ shows NO results?
Where exactly are you using this snippet of code for your application? Is it within the heirachy of WordPress or outside the blog directory. Because if it is not working you will need to add this to the top of each page to render the post from each category:::
<?php // Include WordPress define('WP_USE_THEMES', false); require('./blog/wp-load.php'); query_posts('showposts=7'); ?>hope this helps…i know this is really late…oh well anyone else get stuck or Google picks up the content please reply back..