solostream

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

| October 29, 2007 | 30 Comments

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

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Tags: ,

Category: Code Snippets

Elegant WP Themes

Comments (30)

Trackback URL | Comments RSS Feed

  1. Luigi says:

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

  2. Miriam says:

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

  3. Ryan says:

    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.

  4. Hannit says:

    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)

  5. Miriam says:

    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!

  6. [...] 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;?> [...]

  7. Michaelh says:

    Wouldn’t you need setup_postdata for this to work?

    See
    http://wordpress.org/support/topic/146941?replies=6#post-660221

  8. Miriam says:

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

  9. Murphy Mac says:

    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. Miriam Schwab says:

    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. Murphy Mac says:

    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. Noel says:

    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. Noel says:

    And it still hid the code. :)

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

  14. Miriam Schwab says:

    @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. Drew says:

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

  17. rach says:

    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.

  18. nomercy says:

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

    Great tips …

  19. rik says:

    I used this code and it works great. with one exception.  For some reason it takes the title of the post ( i only have 1 post in the catagory i have selected for use with this code) and places it in to the page title. So now every page on my site has the same title as the post in the side bar?

  20. rik says:

    I added the  hard coded the above code into my template index.php
    it works great but it changes the header titles of all my pages to the title of the first post in the list?
     
    any ideas

  21. I’d been searching for this script all night.  Thank you for posting it!

  22. debater says:

    In my browser, your code is reflected as having a “puzzled” emoticon ;? which is obviously not what you intended (unless your php accepts cute smiley faces as valid syntax!). Would it be possible to put this as a text file download link, so that the formatting is correct? It does not work on my site – it just doesn’t display anything at all.

  23. Thank you for this, it works great. I’m trying to add a thumbnail from each post, but I can’t seem to get it to load them properly, only the last post on the list. This is the code
     
    <.?php $postlist = get_posts(‘category=5&numberposts=5′);
    foreach ($postlist as $post) :? .>
    <.?php $thumb_id = get_post_thumbnail_id(); ?.>
    <.li><a href=”<?php the_permalink(); ?>”.>
    <.?php the_title(); ?.>
    <.br /.>
    <.?php the_post_thumbnail($thumb_id); ?.>
    <./a><./li.>
    <.?php endforeach; ?.>

  24. Diana says:

    How to use this code to show the current category posts, instead suply the category ID?

  25. Sheridan says:

    I added the code to the area that I want my post to show and I’m getting a parse error. Any ideas? I’d also like it to show an excerpt of the post but I’m not sure how to go about that.

  26. Vali says:

    Hi,
     
    I am getting an error. I dont have a list of posts, instead the code creates for me another box with the next link. Check the link for website please. What is worng?

  27. Sheridan says:

    Hi again,
    I was able to get the code working on my site without error! Yea! Now I’m trying to customize it to fit my needs more.
    My clients wants the post excerpt to show after the title. I used the code found on the WP docs to display the excerpt but rather than pulling the excerpt of the post your code is pulling it’s pulling the first post shown on the page.
    I added:
    <?php the_excerpt(); ?>

    Can you help me with this? Thanks
     

  28. jaydev says:

    yah, its weird. using the code above disables the_excerpt and the_content and only shows the_title and i think its just for the index.php
     
    hope someone can have the solution.

  29. nasir says:

    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.

  30. Chief says:

    What widget do you use for your ‘Subscribe’ box?

Leave a Reply




If you want a picture to show with your comment, go get a Gravatar.

More in Code Snippets (26 of 28 articles)