WordPress plugins, themes, tips and hacks

Display a selected number of posts from one category in the sidebar

October 29, 2007 – 2:08 pm | by

<?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.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
  1. 18 Responses to “Display a selected number of posts from one category in the sidebar”

  2. By Luigi on Oct 29, 2007 | Reply

    It does not work in my sidebar…
    maybe something is wrong ?

  3. By Miriam on Oct 29, 2007 | Reply

    Did you change the category number to the right number for your site?

  4. 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.

  5. 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)

  6. 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!

  7. 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

  8. By Miriam on Dec 9, 2007 | Reply

    Hi Michael – it works without setup_postdata. I don’t know why, but it does.

  9. 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….

  10. 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.

  11. 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…

  12. 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…”>

  13. By Noel on Apr 14, 2008 | Reply

    And it still hid the code. :)

    You can see it here:
    http://www.noelgreen.com/code.txt

  14. 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.

  15. By Drew on Aug 15, 2008 | Reply

    Are you familiar with the Advanced Category Excluder plugin? I use it to keep some categories off my front page, and of course the one I want to show in the sidebar is also the category I don’t want on my front page.

    Do you know of anyway I can not have posts show up on the front page but show up on the side bar using this method?

    Thanks!

  16. By rach on Nov 27, 2008 | Reply

    Hey Miriam – I used the code in my topbar and it worked perfectly.  It was exactly what I needed and was so happy to find it after I spent hours searching for such a solution.  Thanks for the post.

  17. By nomercy on Jun 4, 2009 | Reply

    Thank you Miriam … may I have permission to write back on my blog in my language

    Great tips …

  1. 2 Trackback(s)

  2. Nov 8, 2007: Display posts from specific categories on a Page | WordPressGarage.com
  3. Nov 21, 2008: SKULL 1 : Robert Sample

Post a Comment

Revolution Premium Themes