There was an error in the code example that has been corrected. The code will now work in a sidebar text widget.
At the bottom of the sidebar on my other pages like About, I have a list of my recent WordPress posts. Go ahead, go take a look and then come back – I’ll wait.
This is easily done by adding a loop to the sidebar and a query that locates posts from just one category.
Please note that this will only work with standard sidebars. If you are using widget ready sidebars you will want to install the Exec-PHP plugin and then add the code to one of the text widgets. Exec-PHP allows you to run PHP code in posts, pages and widgetized sidebars.
<?php query_posts('category_name=wordpress&showposts=5'); ?> <?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endwhile; ?>
The main part of this is the standard WordPress loop that you will find on most of your template files.
<?php while (have_posts()) : the_post(); ?> <li><a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a> </li> <?php endwhile; ?>
What differs is the query_posts code right before the loop.
<?php query_posts('category_name=wordpress&showposts=5'); ?>
The query_posts code is telling WordPress to locate my last 5 posts in the WordPress category. The loop then runs the code to display them.
This is a great way to highlight the posts in your strongest or most popular category rather than just using a list of all the recent posts.
photo credit: drift-words
Madhur Kapoor says
Thanks Kim, I have been thinking about implementing something like this on my blog. Will try it out.
Madhur Kapoor’s last blog post – Launching GameBojo.com – For the Gamers, By The Gamers
Dave says
Yip, works like a treat. Thanks for another good tip.
Dave’s last blog post – Choosing the right ecommerce platform for your microbusiness
Derick says
Ok, my question is do you get better performance by adding this to the theme versus using a plugin (per your above). I’m assuming that bypassing a plugin/widget is the most direct and fastest way to pull this part of your page.
Kim Woodbridge says
Hi Derick – I think a simple piece of code is almost always a more efficient solution than a plugin. I don’t know if the performance will be better – it depends on a lot of factors such as what other plugins are installed, how many sidebar widgets do you have, what theme are you using, etc. For example, my site loads faster when I remove the Facebook fan box from the sidebar – making that connection to Facebook slows things down.
Kim Woodbridge says
@Madhur – Thanks! Maybe you will find this useful on your new gaming site :-) I like your new photo and I hope you had a great birthday.
@Dave – Hi! I’m so glad you found it useful – and that you stopped by and left a comment.
andymurd says
Kim, you’re becoming quite the WordPress guru. When do you release your first theme?
Keep it up, I’m learning lots.
andymurd’s last blog post – Social Media Best Practices
Kim Woodbridge says
Hi Andy – I know. That’s going to have to be next. :-) How about I’ll design one for you once you get your site on WordPress? ;-)
Thanks!
Ajith Edassery says
Yet another great tip… I guess, you are putting it altogether page by page for a new theme :)
Btw, if theme designing or revamping could also be one of your services (Like balkhis has a ‘pimp my blog’ as well as paid services)
Cheers,
Ajith
Ajith Edassery’s last blog post – Google bashing and Screw Google mentality!
Kim Woodbridge says
Hi Ajith,
Well, Andy started the whole theme thing – I really don’t have time for it right now and it takes me forever … I fuss over every little thing. The tips are just things that I spent time trying to figure out and figured I should write them up and share them with others in way that’s easy to understand.
Thanks!
Kikolani - Poetry | Photography | Blogging Tips says
This is definitely something I want to add in my blog, although I am not sure where yet. But I have it bookmarked in my things to do! Thanks for the tip, great as always. :)
Kikolani – Poetry | Photography | Blogging Tips’s last blog post – The Blogging Process
Kim Woodbridge says
Hi Kristi,
I’m glad you found it useful :-) For some reason I had the hardest time figuring out how to get this to work and once I did I wanted to share it with everybody.
My list of things to do/try in WordPress is way too long.
TheAnand says
Hi,
I was trying something like this without a plugin. Thanks for the code, any heads up on how to do this with all the posts. like make it auto check the present category and display posts on it.? :)
Hope i am not asking for too much…lol
Kim Woodbridge says
Hi,
I’m not entirely clear on what you’re asking. Do you mean show the recent posts by category based on the category of the article being viewed? Or show articles in the sidebar based on the category of the articles being viewed?
Either way this complicated without a plugin. You would need to use conditionals – if statements – if in this category do this, else do this, etc. There’s some information about this here in the Outside the Loop section http://codex.wordpress.org/Template_Tags/in_category and information about conditionals here http://codex.wordpress.org/Template_Tags/in_category
Perishable Press might be a good place to ask you question – Jeff is very knowledgeable about advanced code techniques http://perishablepress.com
The related posts plugin finds similar posts based on tags.
There is a related category plugin at http://playground.ebiene.de/400/related-posts-by-category-the-wordpress-plugin-for-similar-posts/
TheAnand says
yea, i figured out about the if/else, I was wondering if there is a hook to get the current category from the post itself to make it work easier…lol
I was trying to display it on my homepage wordpress installation. Where i can show some 5 recent posts in that category…like photos…I think using your solution is a good way since I have only some 2-3 categories…
thanks for the quick response!
Kim Woodbridge says
There might be a hook but I didn’t find one. If you only have a couple of categories, I do think my code example would be a lot easier :-)
Elke says
Hi, I so need this feature. I’m trying it out in a text widget w/ exec_php, but getting a run time error that apparently means there’s something not right about the php”
Parse error: syntax error, unexpected T_ENDWHILE in /wp-content/plugins/exec-php/includes/runtime.php(42) : eval()’d code on line 6
Thought I’d let you know …
Elke says
I think there are more ENDWHILE’s down the line on my page and that’s why it’s not working. However, I got it to work using the following:
<a href=””>
Elke´s last blog post – A Simplified Gallery with Pixelpost
Elke says
didn’t take the code I posted
but it’s here wordpress.org/support/topic/167045
Elke´s last blog post – A Simplified Gallery with Pixelpost
Kim Woodbridge says
Hi Elke – Additional endwhiles can definitely cause the problem. I use the same exact code in my example on this site so I do know that it works. I’m glad you found an alternative solution in the codex.
Kim Woodbridge says
Hi Elke –
The code works but the error was mine in the above block – I am missing the opening ? with <?php in the first two lines. Once that is corrected it works fine in a widget.
Don says
same error for me too … I used the same solution as above, but removed the offset so I get last five posts under the category … also if you use her solution with offset of one, you need to have more than one post in category or you get a blank
so anyway as I got the same error you may want to explore it … I was using it in a sidebar text widget using the proposed php solution if that helps
Don´s last blog post – Kudo’s to Local Computer Repair Company: Grand Prix Computer
Kim Woodbridge says
Hi Don – I’ll test it again in a widget. Mine is in the sidebar manually – I think I tested it in a widget when I first wrote the article but right now I can’t remember. Thanks!
Kim Woodbridge says
Hi Don,
The code works but the error was mine in the above block – I am missing the opening ? with <?php in the first two lines. Once that is corrected it works fine in a widget.
Tasin Reza says
Many thanks Kim, This was really helpful.
Tasin
Tasin Reza´s last blog post – How to display error message on forms.
Kim Woodbridge says
Hi Tasin – You’re welcome. I’m glad the article was useful.
matthew says
hey, i tried this code and it seemed to work great, but once I logged out of the admin section and just viewed the site the recent post tab was empty. not sure what happened.
.-= matthew´s last blog ..Camp Kingdom! =-.
Kim Woodbridge says
Hi Matthew,
I would need to see an example of the code – it can be placed in the comments between the
tags. If you are using it in a text based widget, are you using the Exec-php plugin? And is the category name correct?
matthew says
also, is it possible to add two post categories instead of just one?
.-= matthew´s last blog ..Camp Kingdom! =-.
Kim Woodbridge says
Hi Matthew,
To display more than one category you want to use an array and use the category id numbers rather than the category names.
Something like
query_posts(array('category__and'=>array(1,3),'showposts'=>2,
'orderby'=>title,'order'=>DESC));
matthew says
I am editing the code to the sidebar.php. here is the code:
<a href=””>
var countries=new ddtabcontent(“tabs”)
countries.setpersist(false)
countries.setselectedClassTarget(“link”)
countries.init()
again, it shows up when I’m logged in as the admin, but otherwise it doesn’t work. I’ll use to array code and see what happens there. Who knows, maybe it will fix it. At any rate, thanks for your help. I’ll let you know what happens.
.-= matthew´s last blog ..Camp Kingdom! =-.
matthew says
Hm, let’s see if this works.
<a href="">
var countries=new ddtabcontent("tabs")
countries.setpersist(false)
countries.setselectedClassTarget("link")
countries.init()
.-= matthew´s last blog ..Camp Kingdom! =-.
matthew says
i apologize, apparently I don’t know how to embed code. I’ll try once more. I took out all of the . Hopefully this works.
div id=”tabsWrap”
p id=”tabs”
a href=”#” rel=”tab1″ class=”selected” ?php _e(‘Recent Posts’); ? /a
a href=”#” rel=”tab2″ ?php _e(‘Recent Comments’); ? /a
/p
ul id=”tab1″
?php query_posts(‘category_name=kingdom-concepts&showposts=5’); ?
?php while (have_posts()) : the_post(); ?
li a href=”?php the_permalink(); ?”
?php the_title(); ?
/a /li
?php endwhile; ?
/ul
ul id=”tab2″
?php include (TEMPLATEPATH . ‘/simple_recent_comments.php’); ? ?php if (function_exists(‘src_simple_recent_comments’)) { src_simple_recent_comments(3, 100, ”, ”); } ?
/ul
script type=”text/javascript”
var countries=new ddtabcontent(“tabs”)
countries.setpersist(false)
countries.setselectedClassTarget(“link”)
countries.init()
/script
/div
.-= matthew´s last blog ..Camp Kingdom! =-.
Kim Woodbridge says
Hi Matthew – I emailed you and it seems that you got the array to work.
vamsi says
friends in my home.php if i use
then the page numbers plug in not working,
page numbers are showing but if click on 2nd page it showing againg 1st page
Kim Woodbridge says
Hi Vamsi – I’ve learned that query posts can be very fussy and cause that problem. You will want to use something like this
Mecanographik says
Hi, if you want to use a “recent post function” for only one particular category here is a my example :
<?php
// example here for the 7 most Recent posts for the category 5
$output = '';
$before = '';
$after = '';
query_posts('showposts=7&cat=5');
while (have_posts()) : the_post();
$post_title = stripslashes($post->post_title);
$permalink = get_permalink($post->ID);
$output .= $before .'' . htmlspecialchars($post_title). '';
$output .= $after;
endwhile;
echo $output;
?>
Instead you have got this function mdv_recent_posts(number of post);
but it doesn’t work with only one category exclusion …
.-= Mecanographik´s last blog ..links for 2009-10-08 =-.
Kim Woodbridge says
Hi – Thanks for the code example. I’m going to try it out. :-)
Aery says
Hi Kim,
Thanks for such wonderful trick.
just One Question – How do I implement it with Tags?
I mean, like you have shown latest posts by categories, I would like to do same for my new theme with the help of Tags.
Can you please take out 2 minutes to figure this one out. I am sure you can.
Regards.
.-= Aery´s last blog ..Best 10 Gadgets for Gmail that are Damn Cool and Useful =-.
Kim Woodbridge says
Hi – I’m not sure. I would have to work on it. There is a plugin that might help you out though.
http://sudarmuthu.com/wordpress/posts-by-tag
Shawn Hooghkirk says
Kim,
Great stuff! I did a quick search on Google and found your post on the first page and it did the trick. (search: list category posts wordpress) <– Just thought you would like to know :)
Anyways I'm very grateful that you put this up!
– Shawn
Kim Woodbridge says
Hi Shawn – Thanks! There are a couple of search phrases that give me good results. And I’m really glad the article helped you out :-)
Love says
Works great, thank you!
Arun Basil Lal says
Hello Kim,
I was confused with the code (bad memory), I googled it and see where I landed :)
Thanks
.-= Arun Basil Lal´s last blog ..Bandwidth Monitoring Tool for Windows Vista and Seven =-.
Kim Woodbridge says
Hi Arun – I’m glad to know I’m being found through google ;-)
Roger says
Thank you for the code. I used it on a client’s site and it worked perfectly!
.-= Roger´s last blog ..The most compelling reason to hire outside help in planning and implementing a social media strategy =-.
Kim Woodbridge says
Hi Roger – Thanks – I’m glad it helped you out.
tigris says
works like wonder on my blog, thanks! :D
Kim Woodbridge says
Hi Tigris – Great! I’m glad that it worked for you.
Marc says
Helped me a lot! Thank you very much! :D
Kim Woodbridge says
Hi Marc – Great! Thanks for letting me know.
yesMan says
Thanks for the code, it’s working great.
However, I do have an issue with current-cat. The category in the code is always highlighted regardless of which category from wp_list_categories being displayed. Simple solution is of course not using the current-cat, but I want the active category to be highlighted, except the category in the code.
I’ve tried to exclude the category in wp_list_categories but it didn’t do the trick. The category is gone but still no current-cat for other categories.
Any pointers to this? Thanks!
Kim Woodbridge says
Hi – Can you style .current-cat or #nav .current-cat
There is an example here
http://www.livexp.net/wordpress/highlight-the-current-category-of-wordpress.html
Julian says
Hi Kim
This did exactly as you said, thanks! It is the “Current Discounts” listing of posts I didn’t want on my blog main page (chandelier-mall.com/blog).
One thing I would like to add is to include a short (and not hyper-linked) content extract under each link. I s that something you know how to do?
Thanks for this great resource!
Best wishes
Julian
.-= Julian´s last blog ..Lighting by Gregory Consistently Provide Great Value Discounts =-.
Kim Woodbridge says
Hi Julian – Below the area where you write a blog post is a section called Excerpt. If you add a sentence or two about the content of the post in that section, you can then call it in your sidebar code.
Under <?php the_title(); ?> add
<? php the_excerpt(); ?>
that will add the little blurb that you added to the excerpt field.
Julian says
Hi Kim
Sorry to be slow in showing my appreciation – all the volcanic ash has left my partner trapped in India so I’ve been pre-occupied somewhat! So – many thanks for your quick response.
A couple of things:
– I had to remove the space between the ? and “php the_excerpt” – then it worked.
– What I was really wanting was for the extract text to NOT be (blue) hyperlinked, but to be in plain text.
– In fact, to refine this even more, I would also like to NOT have the whole of the title hyperlinked.
For instance, on the left sidebar of chandelier-mall.com/blog/ under Current Discounts, one of the links is presently “Lighting by Gregory Consistently Provide Great Value Discounts” which becomes 3 lines of hyperlinked blue text, which I don’t really like. What would be great would be to have, say, the first 3 words (Lighting by Gregory) as the hyperlinked anchor text and then the rest of the title (Consistently Provide Great Value Discounts ) makes a reasonable description by itself.
Am I pushing my luck with these 2 requests?
Actually, I’m starting to appreciate the php language more as I start to fiddle with it!
Best wishes and thanks again!
Julian
.-= Julian ´s last blog ..Eco-Alerting Blogs – Light for Home and Heart =-.
Kim Woodbridge says
Hi Julian – Wow! Trapped in India – hope everything works out ok.
The code is going to pull the entire title – it doesn’t know how to break the title into the pieces that you want. But if you remove the permalink code surrounding it, it won’t be linked.
Julian says
That works brilliantly Kim – many thanks! I can always adjust titles to keep them short, and tailor the excerpt to get what I want!
Katja’s back from Delhi now. Aeroflot excelled themselves – 5 nights in 5 star hotels all paid for; then suddenly an announcement that they were flying out in a few hours – and it all ended quickly and very smoothly!
.-= Julian´s last blog ..Celebrating The 2010 Milan Design Week =-.
Kim Woodbridge says
Hi Julian – I’m so glad the code worked and that Katja got home safe and sound :-)
Sheridan Broderick says
THANK YOU SO MUCH! I added the_excerpt() ; to collect the post excerpt and your code works marvelously! I tried a few other snippets but they wouldn’t work with the excerpt piece.
Thank you again so much! I will definitely be subscribing to your blog!
I have one question though. Each of the posts within this category are going to have a picture to go along with it. That picture is the first thing in the content of the actual post. Do you know how to get that thumbnail to show up in the excerpt?
Thanks!!! =)
Kim Woodbridge says
Hi Sheridan – Images don’t work with excerpts by default. You might want to look into the thumbnails for excepts plugin. This plugin, however, will add the first image in a post to every excerpt on your site.
http://www.kimwoodbridge.com/wordpress-how-to-improve-the-archives-with-the-thumbnails-for-excerpts-plugin/
You can also take a look at post thumbnails in 2.9+ but off the top of my head I’m not sure if they work with excerpts.
http://www.kimwoodbridge.com/how-to-add-the-post-thumbnail-feature-to-your-wordpress-installation/
And then there is the custom field method.
http://perishablepress.com/press/2008/12/17/wordpress-custom-fields-tutorial/
Not exactly a quick answer to that one. :-)
Sheridan Broderick says
Thank you Kim!
I did actually find a solution that involved thumbnails. The site is now pulling the info correctly. http://www.immigrationmagazine.com! (sidebar)
— Thank you again!
Danielle says
Good info… But what about a Tab that goes to a page with only only category’s posts on it?
Kim Woodbridge says
Hi Danielle – Well you could link to a category page for that specific category or you could make a page template that only pulls posts from that category.
blog tools says
Thanks this has been very helpful.
I’ve been trying to get recent posts from the category of a single post in single.php.
Anyone happen to know how to do that?
Kim Woodbridge says
I think the Related Posts by Category plugin would do that for you.
http://wordpress.org/extend/plugins/related-posts-by-category/
Ryan says
Hi Kim – is there something I can do with the second loop to stop it from affecting the main loop?
Here’s what’s happening —
On index.php I have the main loop pulling the four latest posts. In my a sidebar widget I have your custom loop pulling the last post from one category. The idea is that this loop will feature pre-programmed posts that will publish on a daily basis.
Unfortunately what’s happening is that with each day, and each new post displayed in the sidebar, a post from the main loop is pushed from index.php onto the archive page. The same thing happens with the archive pages — posts get bumped to a second page. After four days, and four daily posts, and no new posts in other categories, index.php features no posts!
Is there a way to stop this from happening? Thank you for your fine work!
Kim Woodbridge says
Hi Ryan,
I think you need to use a reset query after your custom one.
<?php wp_reset_query() ?>
Ryan says
Hi Kim, thanks for the quick response! I think what’s causing the issue is that I’ve asked for only four posts to display on index.php. I have a plugin (Clix Category Exclusion) keeping the daily, sidebar loop posts hidden from the main loop. So what’s happening is index.php will only display four posts, but the posts that are supposed to be hidden are bumping an older post from index.php and not replacing it. I suppose there is an work around for this, but it seems like a cause and effect type thing that may not be fixable!
Ryan says
Hi Kim – you know what, I figured it out. I’ve switched plugins, and the Simply Exclude plugin seems to work much better — no issues with your loop idea. THANKS!
Kim Woodbridge says
Hi Ryan – I’m glad you found what was causing the problem :-)
Anthony says
Kim this not working for me. I added Exec PHP and then added the code to a text widget in the sidebar and when I click on the category I get the same error.
Here is the error message:
“This webpage has a redirect loop.
The webpage at http://www.discountgymequipment.org/abdominal-exercise-equipment/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.”
Any help would be appreciated
.-= Anthony´s last blog ..Why SocialADR Might Be The Best Social Bookmarking Tool Yet? =-.
Kim Woodbridge says
Hi Anthony – I’m not sure what is causing that. Something else is conflicting with the code – do you have any other redirects set up?
Anthony says
Not that I am aware off. I just started this site a week ago, and only been adding content to it.
.-= Anthony´s last blog ..Why SocialADR Might Be The Best Social Bookmarking Tool Yet? =-.
Kim Woodbridge says
Hi Anthony – Without being familiar with the site, I really don’t know. The code has been tested in widgets so it should work. Something else must be interfering with code.
Anthony says
Kim, so i trying to give this another shot.
I have the Exec-PHP plugin activated. I am using a simply clean theme called WPSimpy and it is widgetized.
I placed the code in a text widget and added it, saved it.
Now I view my site, and when I click on any category listing, i get the same error message.
If you can take a look at it for me and tell me what might be the problem, I would highly appreciate it.
(I have no redirects set up for this domain)
.-= Anthony´s last blog ..Bowflex PR1000 Home Gym =-.
Kim Woodbridge says
Hi Anthony – If you look at the bottom of my left sidebar there is a list of 5 recent posts in the WordPress category. I copied my code and pasted it into a widget – as you can see, it’s working properly.
I went to your site but didn’t see a sidebar block with this code so I can’t tell what is happening. Maybe there is something in your .htaccess file or maybe you have a directory with the same names as the category – although that usually causes a 403 error.
Anthony says
thankyou Kim,
I wonder if this is a problem my ISP could fix? I will give them a try
.-= Anthony´s last blog ..Bowflex PR1000 Home Gym =-.
Aery says
I am Surely Going to Use This
.-= Aery´s last blog ..What is “Sort by Magic” in Google Reader? =-.
Kim Woodbridge says
Hi Aery – Great! I’m glad it helps.
Adrian Mirea says
Thanks, this was very helpfull for me.
But there is a minor problem… At the end you should call wp_reset_query(); otherwise this script might affect (as in break) other queryes existing in the current page:
<a href="”>
<?php endwhile;
wp_reset_query();
I used this on the main page to display the first 10 posts in a certain category, and if i had not called wp_reset_query() then the rest of the page would break.
Thank you and God Bless.
Kim Woodbridge says
Hi Adrian – Yep, you are right. I didn’t learn about reset_query until well after I had written this article. :-)
Mark says
Thanks Kim for the great tip and thanks Adrian for the reset_query idea. Put together, I am able to do exactly what I want on my site!
Kim Woodbridge says
Hi Mark – I was so happy when I discovered reset query – it solved so many problems that I was having ;-)
Ipstenu says
Hah! Perfect. This is just what I needed today!
.-= Ipstenu´s last blog ..Taffy Hates LeBron James =-.
Kim Woodbridge says
Hi – Awesome!
javier says
Hi Kim!
Thanks por that, I have two questions
1. I put the outcome of an image code to add a little design.
2. What happens if I show an image thumb and below the title of the post.
.-= javier´s last blog ..¿Que nos lleva a ser exitosos =-.
Kim Woodbridge says
Hi Javier – If the code is accurate, it should work without any problems.
Nicole says
I tried the solution that you suggested to “blog tools” i.e. to use the plugin at http://wordpress.org/extend/plugins/related-posts-by-category/
It really looks like a perfect solution but I can’t work out how to use it :(
I installed the plugin but could not find an explantion on what code to put in the post in order to get the list of posts and their thumbnails to appear. I am so sorry to send this to you but I could find no way to ask the question on the site that you refered to. If you could explain this it would be much appreciated!!! Thanks so much for a great useful site!
.-= Nicole´s last blog ..Special Requests for Reward Charts =-.
Kim Woodbridge says
Hi Nicole – The code example on the plugin page will work. You have to edit the single.php template file – this is in WordPress under Appearance > Editor > single.php If you aren’t comfortable editing template files, this might not be a good solution for you.
Nicole says
Thank you so much for your reply. Does this plugin appear on every page. Do you know of another solution that will enable me to have this list appear on particular pages? I thought that this is possible with the plugin but I guess not.
Kim Woodbridge says
Hi Nicole – That plugin is designed to work on the single posts page so it will shop up with each individual post. It is based on categories so it won’t work on pages.
Ethan says
Hi Kim,
Thanks for this excellent post.
Quick question. What do I do if I want to use this on a page that does not have blog posts?
I put this code in my header, and it transforms all of my WordPress pages into blog pages.
Does that make sense?
Kim Woodbridge says
Hi Ethan – It should work on any page. I use it in the sidebar on other pages that don’t have blog posts. An example is the Recent WordPress Posts and Recent Facebook posts in the sidebar on the Services page.
http://www.kimwoodbridge.com/services/
John Tanedo says
How do you display latest posts on sidebar except for the one being currently viewed?
Kim Woodbridge says
Hi John – I’m looking into this – it’s not as easy as it sounds. I’ll let you
know what I find out.
sid says
Hi Kim
I just used your code. Its cool. But there is a problem.
If I use the mentioned code; tags go beyond control. I mean if you clicks on any of the tags they show all the posts in category .
is there any fix.
Kim Woodbridge says
Hi Sid – Are you using this in the sidebar or the main template page? Is the code for the tags within the loop in this article. Do you have more than one loop on the page? If so, you will want to use
wp_reset_query();
as mentioned in the comments.
Hannah says
Hello and thank you!
I’ve been looking for something like this for a while, glad you posted this.
Just one question — how do I do it so instead of just showing the 5 recent posts from each category, it shows all the posts.
This might be silly but I tried instead of putting showposts=5, I put showposts=all and it didn’t work.
Help! Thanks again.
Kim Woodbridge says
Hi Hannah – Leave off the &showposts= part completely. Just request the category and it will pull all of the posts.
Chip says
I, like many others, found you via google and a couple years after the original post :)
Works great! I do, however, have a request to modify it. I’m actually using this on a page that is pulling the most current post from a particular category. Is there a way to modify your code to remove the link for the post that is already lsted on the page.
Hard to understand what i’m talking about without seeing it: http://www.nwtechanddesign.com/diy
Kim Woodbridge says
Hi Chip – I’m sure there is but I don’t have the code of that. You might want to search and see if you can locate it on another site. This article is close but isn’t exact.
http://www.wpbeginner.com/wp-themes/how-to-exclude-latest-post-from-the-wordpress-post-loop/
Lee says
Hi Kim,
Great code, and good on you for answering everyone’s questions as well as you have!
Now I hope you can help me too…
I have the code in my Sidebar and I would like to display the latest posts for whatever category the user happens to be in *right now*.
Basically, is there a way of changing:
category_name=SpecificHardCodedCategory
to
category_name=ThisCategory
Or even better,
category_name=ThisPage
…as I have my Pages set up to display Posts whose Category share the same name as the Page.
Lee says
Okay so I figured it out based on the original Loop I’m using to actually display posts.
I changed the line to say:
query_posts(‘category_name=’.get_the_title().’&showposts=10′)
And it works fine now for whatever category I’m in.
Kim Woodbridge says
Hi Lee – Excellent! I’m glad you got it to work and thanks for the code.
Pushpak says
Can anyone please tell me how to display list of posts from just 2 categoriesonly.
I m having 5 categories , in a page i want to display the posts from 2 categories i.e ‘heart’ and ‘cancer’ only.
Any help will be greatfull.
Thanks in advance
Pushpak
Kim Woodbridge says
You could try excluding the three categories that you don’t want to appear.
query_posts( ‘cat=-1,-2,-3’ );
Techie says
you made my day kim. i wasted lot of time to get it done but now i did it within 5 sec after copy/pasting your code.
Thanks alot.
ionrane says
Hello.
Nice Post, Nice Site.
I, also found you via google.
I have been working on this problem for 2 days, and still cannot get it to work.
At the top ( or bottom) of the “Members” page, I want to display just the lastest post in the category “Meetings” (cat_ID=10), in order to display the date/time/infor for the Next Meeting.
I am using the code you have above, [ query_posts(‘showposts=1&cat=10’); ], and many variations, including some that use array. I have also added [wp_reset_query();] But I cannot get it to work !
I can get Either 1 post, the latest post, irrespective of category,
or I get All posts in the meetings category.
But it will not honor both !
Has anything changed in wordpress since this article. I have run across several other comments at various websites that report similar troubles.
Otherwise, any suggestions as to what might be causing the fail?
Any direction to look into is soo greatly appreciated.
I am using wordpress 3.1.3, php5
I have tried this in 2 themes, that are about 2 years apart in publication date.
thanks in spades to anyone who can help me along in this path !
Kim Woodbridge says
Hmm … are member posts restricted in some way? That is the only cause for this problem that I can think of so far.
universal web says
I am trying to use this code on a page. It shows the list of posts as expected, but also the posts content at the bottom of the page which I do not want. I am not sure why this is happening. any Ideas?
Thank you for a great tip and your site is awesome.
Kim Woodbridge says
You may want to place a reset query at the end of your code.
http://codex.wordpress.org/Function_Reference/wp_reset_query
oran says
How to generate a report / list of all post ID in the word press blog?
Sunil Rana says
Finally i got good article on how to get other posts from same category in wordpress. It worked for me and life saver !!.
http://www.phptechi.com/how-to-get-other-posts-from-same-category-in-wordpress.html
Joe says
many thanks!!!! its works! :)
vishwa says
First thanks for the info. I am trying to fetch content from two posts side by side manner. but it goes into looping and the two post contents keeps loading N number of times.
How to stop looping? could you correct and give me the code. As of now on my website i have kept the left handside content (current issue), beside the current issue another post content will displayed (Icon of the month)
[code]
/* left hand side content fetched from post under category “Current issue” */
Current Issue
<a href="” title=””>
/* right hand side content fetched from post under category “icon of the month” */
[/code]
Relevant Question Asker says
This seems to break pagination; you get a button saying “older posts” but it leads to the same page.
Relevant Question Asker says
This works and doesn’t break pagination:
http://wordpress.org/support/topic/query-posts-and-pagination-links-broken
Shubham says
thanks.. was looking for this for a site I am working on! :) have a great day!
Julie says
Ok for some reason when I added this to my sidebar, the only posts that would show up on the main blog area were from the category selected in that script. Anyone had that issue?
Ricky1990 says
Hi,
Thanks for great post ! But it doesn’t show thumbnail. You should add functions to show thumb.
Dubai web design, development says
How do you display latest posts on sidebar except for the one being currently viewed?
Ian Garcez says
Hi! I would like to tell about my extension, it is a shortcut to get this working. It still isn’t the easiest to use but, please give it a shot. I’m still developing it, adding new options, so please feel free to complain about it.
http://wordpress.org/plugins/onespace-lastpost/
srry for using your comment space to do this, is there any way that I can refer your website?
thanks!