Display a selected number of posts from one category in the sidebar
October 29, 2007 – 2:08 pm | by Miriam Schwab<?php$postlist = get_posts('category=6&numberposts=5');
foreach ($postlist as $post) :?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php endforeach; ?>
Use this in the sidebar to display a defined number of posts from a specified category. Where it says “category=6″ replace the 6 with the relevant category ID number, and where it says “numberposts=5″ replace the 5 with the number of posts that you want to appear.
Update Nov. 7, 2007:
The above code snippet can also be used to display a page in the sidebar. This is is handy if you want to create an editable chunk of text in the sidebar without using widgets. So, for example, if you want to show an excerpt of your About page in your sidebar, use the following code:
<?php$postlist = get_posts('id=54');
foreach ($postlist as $post) :?><?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">read more</a>
Where it says id=54, replace 54 with the id number of the page you want to display.



14 Responses to “Display a selected number of posts from one category in the sidebar”
By Luigi on Oct 29, 2007 | Reply
It does not work in my sidebar…
maybe something is wrong ?
By Miriam on Oct 29, 2007 | Reply
Did you change the category number to the right number for your site?
By Ryan on Oct 30, 2007 | Reply
Miriam. Do you know how to display all of your category titles, with full posts underneath? So just like what you’ve got above, but for all of your categories. I can do it manually by adding each category one by one, but I can’t get it to do it automatically so that when new categories are added they appear on the page.
I had a use for this recently, but couldn’t nut out how to do it.
By Hannit on Oct 30, 2007 | Reply
you’ll probably want to add the_content() or the_excerpt() inside the loop when used in the main loop (i.e. not the sidebar)
By Miriam on Nov 7, 2007 | Reply
This code only works in the sidebar. Tomorrow’s post will be about how to display specific categories in the main content area, so stay tuned!
By Michaelh on Dec 9, 2007 | Reply
Wouldn’t you need setup_postdata for this to work?
See
http://wordpress.org/support/topic/146941?replies=6#post-660221
By Miriam on Dec 9, 2007 | Reply
Hi Michael - it works without setup_postdata. I don’t know why, but it does.
By Murphy Mac on Mar 6, 2008 | Reply
Hey Miriam -
This is exactly what I’m looking for. But I get an error that says
<pre>
Parse error: syntax error, unexpected T_VARIABLE in (followed by path to my sidebar.php)
</pre>
Any idea what that could be about? I’m not to bright with Wordpress….
By Miriam Schwab on Mar 9, 2008 | Reply
Murphy Mac - it seems that the problem may be a result of copying and pasting directly from this blog into your theme, which is preserving the styling I applied to the code snippets above to make them look like code. Try to copy the text from here into a text editor of some kind that will strip out all the styling, and then copy it from there into your theme.
Let me know if that helps. Sorry it took me so long to respond - I’ve been swamped with work.
By Murphy Mac on Mar 10, 2008 | Reply
I wish I knew more about this stuff!
I took it through a plain text file and got the same result.
I’m on Wordpress 2.2 - but I’ve downloaded a couple of the newer files for security reasons. But I haven’t updated my entire installation. Maybe that’s the problem?
Thanks for the reply…
By Noel on Apr 14, 2008 | Reply
I’m trying to use your code in conjunction with the “Post Image” plug-in, and it’s not giving me any output.
Any suggestions?
Here’s the code… (I put spaces between the < and ? on the php so as it would show up for sure.)
<a href=”" rel=”bookmark” title=”Buy this…”>
By Noel on Apr 14, 2008 | Reply
And it still hid the code.
You can see it here:
http://www.noelgreen.com/code.txt
By Miriam Schwab on Apr 24, 2008 | Reply
@Noel - Sorry about the hidden code. Comments here can misbehave quite a bit. As for the problem with your code - I can’t see anything wrong with it. Sorry.