solostream

Display recent posts in WordPress sidebar

| November 11, 2007 | 65 Comments

Here’s a bit of code to put in your sidebar to display the most recent posts on your blog:

<?php $myposts = get_posts('numberposts=10&offset=1');
foreach($myposts as $post) :? >
<li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
<?php endforeach; ?>

You can set the number of recent posts to appear on the first line of code where it says numberposts=10 – change the 10 to the number of posts you want to appear. You can choose to offset the recent posts by any number; for example, if you select offset=1, as we did above, it won’t display the most recent post, but will start the list from the second-most recent post.

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 (65)

Trackback URL | Comments RSS Feed

  1. Anna Vester says:

    Nice tip! Thanks for that. I will be implementing this on my blog soon as well.

  2. [...] Display Recent Posts in WordPress sidebar and Hard-coding an SEO friendly title in the WordPress header file – Miriam posted some very simple and powerful tricks. [...]

  3. Kim says:

    Awesome! Thank you!!!!

  4. Ryan says:

    I implemented this in a new theme of mine today.

    Although "php$myposts" should be "php $myposts"

  5. Miriam Schwab says:

    Ryan – thanks for the correction! I’m fixing it now.

  6. Li says:

    oh thanks a lot for your tips!!!!! :) I’m not good at php myself so this is totally my saver!!

  7. vel says:

    hey im just wondering how you would edit the code to be able to put a heading at the top of the list that actually says “Recent Posts” or whatever?

  8. Ryan says:

    Vel – You just need to add the HTML before the script. Something like the following would work, however the exact layout would depend on the individual sidebar as themes can format their sidebars in many different ways.


    <li><h2>Recent Posts</h2></li>
    <?php $myposts = get_posts('numberposts=10&offset=1');
    foreach($myposts as $post) :? >
    <li><a href="<?php the_permalink(); ?>"><?php the_title();?></a></li>
    <?php endforeach; ?>

  9. Phanakapan says:

    I was wondering if there is a way to make this work for posts from a separate wordpress install. I have my blog on a sub URL, but would love to be able to show the post titles in the sidebar on my homepage. thanks for any help you can offer!

  10. Miriam Schwab says:

    @Phanakapan – To display recent posts from another blog installation, you should use an RSS display method, that publishes your RSS feed from your other site.

  11. [...] WordPress Super Loop explains how to exclude posts in a specifica category from the main page.  WordPress Garage’s Display Recent Posts in the Sidebar explains how to show recent posts in the sidebar.  And finally, we modified the code from WPG to [...]

  12. urdubachi says:

    hi dears i have a this website, i want side bar thumbnails of my posts pictures how can i setup this ………

  13. Hello Miriam,
    Thank you so much for this invaluable code!!!! Wow, exactly what I was working for! Congratulations on a job well done! I am working on a site using this technique – http://www.ilionfilmcompany.com. Keep up the great work and I will visit your site often now…
     
     
     
     

  14. Exactly what i was looking for to add on one of my other sites.  Nice, quick and easy to the point, and customizable.

    You’re awesome!

  15. Jason says:

    The wp_get_archives() template tag already does this:

    wp_get_archives(‘type=postbypost&limit=10′);

    It will print out a bunch of li’s with the title; linked and everything.

  16. Miriam Schwab says:

    Jason – you’re right, and I can’t remember exactly why I published this bit of code. The only thing I can think of is that this code allows you to offset the recent posts, which is an advantage that the wp_get_archives tag doesn’t offer.

  17. Miriam Schwab says:

    @Link Building Bible – no, you’re awesome! Thanks for saying thanks.

  18. Sachin Garg says:

    Thanks, my upgrade to wp 2.6 broke my old code for doing the same job. Your code works like a charm :-)

  19. Klaus says:

    Thanks

    I have implemented this in order to add a class to the <li>-tags

  20. Martin says:

    Hi Guys,

    Thanks for posting this, im having a tiny problem though and Ive been trying for hours to fix it but I cant get it to work, its basically showing the first 10 blog posts on my blog and not the ten most recent, does anyone know why this maybe? this is using the exact code shown above.

  21. 5ph3x says:

    Thx a lot for this code..really helped my site..applied it here http://bryanking.net/

  22. Simon says:

    Thanks for the code:) Using it on the 404 page of my blog at <a href=”http://www.blizzardboy.net”>Blizzardboy: a Kiwi in Japan</a>.

  23. Martin says:

    Just incase anyone has the same error as I previously posted I fixed this by updating the wordpress software to the latest version.

  24. What do I need to add to make this *not* appear on the homepage but to appear on single posts and cats etc?

  25. sandi says:

    why its not working in my blog?

  26. Anybody know how to do this and also automatically include the *date* of each post listed in the recent posts?

  27. Jay Robinson says:

    This is a great tip, and I am using it on the front page of Fountain Hills Update.com. Thanks!

    P.s. What would be the easiest way to display recently modified posts?

  28. Angela says:

    Thanks for this great tip!  It’s saved me a real headache in trying to figure this out for myself.

  29. Ryan says:

    Jay – Displaying your most recently modified posts would be very complicated I think. I assume you would need to start querying the database directly to try and track down the dates on your post revisions.

    You would definitely need to be running WP2.6 too, as I’m pretty sure that sort of data wasn’t stored in older versions.

  30. Kok Choon says:

    Thanks for the code! My theme does not support upper sidebar widget, this code help me to do it!

    I just install it on my blog, looks great! Thanks!

  31. jazr says:

    love this thanks.

    is there anyway to get an excerpt of the post with the heading?

  32. Ryan says:

    jazr – The following script will display a link like the above example does, but should also display an excerpt in a seperate list item below it. I haven’t tested it, but I assume it will work.

    &lt;?php $myposts = get_posts(‘numberposts=10&amp;offset=1′);
    foreach($myposts as $post) :? &gt;
    &lt;li&gt;&lt;a href=”&lt;?php the_permalink(); ?&gt;”&gt;&lt;?php the_title();?&gt;&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;?php the_excerpt(); ?&gt;&lt;/li&gt;
    &lt;?php endforeach; ?&gt;

  33. Ryan says:

    Hmm, it seems this wysiwyg box escapes code automatically, but since I escaped it already it showed my escaped code instead :(

    Here is the correct code:

    <?php $myposts = get_posts(‘numberposts=10&offset=1′);
    foreach($myposts as $post) :? >
    <li><a href=”<?php the_permalink(); ?>”><?php the_title();?></a></li>
    <li><?php the_excerpt(); ?></li>
    <?php endforeach; ?>

  34. [...] WordPressGarage.com Cagintranet Web Design [...]

  35. Chris says:

    I just added your code to my blog at http://www.dating-photos.com but how do I change the color of the links? Also, I’d like to get rid of the bullets.
    Thoughts

  36. M says:

    Hey thats awesome, worked perfectly on my page!! yay thanks

  37. Keith says:

    Thanks!  This solved my issue.  Starting a blog, Recent Posts are a lot more important to show than Categories (unless you want to show all 2!

    I am adding you to the eMarketing Search Engine on my site!

  38. Craig says:

    Great it works! Thanks! The only problem is my background is red and so I cannot see the text. I have no clue on php what so ever so not sure how to edit the text colour?

  39. thank you so much! i got it work!

  40. rajasekharan says:

    Thank you. This post was very useful.

  41. Just what I was looking for too. I tried searching for hacks for the widget with no luck, but your code replaced it and made it easy. Cheers!

  42. daraseng says:

    Thanks for your tips…but I don’t where should I put this code in which file…?

  43. Vikram says:

    Thanks for tips.
    but Jason tip  really worked for me.
    I got exactly what I was looking for. Nice post!!

  44. karl says:

    Shenanigans, my browser is displaying an emoticon in part of the code, creating a syntax error and I’m not sure what comes after ($myposts as $post) and before >  :$

  45. Thanks a lot Miriam for the amazing tip but could you please tell how to target a specific category?
    Thanks,
    - Mohammed

  46. @Mohammed Check out this other post here on WP Garage that explains how to display a selected number of posts in the sidebar from a specific category: code-snippets/display-a-selected-number-of-posts-from-one-category/

    Is that what you meant?

  47. [...] WordPress widgets are delightful things where themes designed for public consumption are concerned. There are situations, however, where widgets aren’t in use or not practical/necessary. The code below can be added to your sidebar (or anywhere in your template, really) to display a list of current pages. I found this on an old (2007!) post at WP Garage. [...]

  48. Jamar says:

    Assalaamu Alaikum,
    Thank you for sharing this code, it was very helpful and is simple to implement.
    May Allah reward you for helping others!
    Salam

  49. Jamar says:

    Well that is truly a shame sister.
    Again thanks.
    Peace be with you.
    ;)

  50. Joni Mueller says:

    Just FYI, this code works with latest versions of WordPress; I’m using it on my inner pages, http://www.pixelita.com/about-us and so forth:
     
    Substitute [ and ] for left and right carets in code below.  The number “10″ can be replaced by however many posts you want displayed; I chose only one, the latest one. :)
     
    [?php wp_get_archives('type=postbypost&limit=10'); ?]

  51. Rizwan says:

    Thanks!
    its working!!!!!

  52. Justin says:

    wp_get_archives(‘title_li=&type=postbypost&limit=1′); works for displaying the title of each post, but what about showing the description of each post?

  53. Thanks! I was looking for thist to! It’s working!!!

  54. Arto says:

    Hi, thanks for this.
    One question: Is it possible to add a “previous posts” lik in the sidebar? And that these previous posts also appear in the sidebar after clicking the link?
    Would be great!

  55. Katie says:

    One problem with the above code is when you call that function in your sidebar which is called before the start of the loop in single.php. Your single.php file will list down all the articles of your blog post irrespective of the one which is called.

    In such cases, you can also use

    <?php wp_get_archives(‘title_li=&type=postbypost&limit=10′); ?>

    This will list down latest 10 posts in ul, li format. This is bit easier.

  56. How would I add these links to a my sidebar on pages outside of WordPress, but on the same site?

  57. Panoz says:

    Unfortunately this is not working for me….
     
    I have tried several variants of the above code and its still doesnt work.
    The difference is that I’m not using the code into the widget but to the php file.
     
    My current command is
    <?php wp_get_archives(‘type=postbypost&limit=5′); ?>

    but this it displays the latest 5 posts which are already displayed in full on my home page.
     
    Any help?

  58. jdk says:

    Not working :(

  59. jornes says:

    Hi Sir,
    I’ve tried to put this code at my footer part, but it doesn’t work for me. Is there any solution? The theme that i using, the side bar is under my post space one.

    But i need this code for recent post badly.

    Hope to get a solution for my blog. Although my blog is in chinese characters. But you are feel free to take a look at my blog layout.
    http://www.fall4lonely.com

    I succeed to moved the categories code to my footer part. Hope that i can have recent post as well. =) 

  60. jornes says:

    The code not working.

  61. April Hodge says:

    Hi, I am needing to put the top 5 latest posts of phpbb in my wordpress theme footer, with this code how do i implement it and where do I link the forum webpage? HELP!! very important asap!
    This is the website I will be using it on http://www.makeupadviceforum.com

  62. MiX says:

    April Hodge,
    Wasn’t that you have that feature at the footer? 

  63. vivek says:

    I think it is outdated

Leave a Reply




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

More in Code Snippets (21 of 28 articles)