ThemeLab's Blog

Stay up to date with our newest WordPress themes, WordPress plugins, WordPress tutorials, and other announcements.

The Easy Way To Display Recent Posts in WordPress

Last Updated on by

You don’t need any custom queries or database calls just to get a list of recent posts. In fact, there’s a function built into WordPress that will do just that. It’s called, wp_get_archives(). You might recognize that as a way to display monthly archives, but with a few parameters, you can modify it to display a list of recently published WordPress posts. Here’s the code:

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

What this will do is strip any WordPress added formatting and output a nice list of the 10 latest posts, each within it’s own <li> tag.

About

Leland Fiegel was the original founder of ThemeLab. He is a web developer who loves WordPress and blogging.

  • http://imanto.com Anto

    You should do the same for recent comments :P

  • http://www.wplancer.com BANAGO

    I don’t think this can be done for comments.

  • Leland

    There is a way actually, not as easy as this though. I’ll make a post soon about doing recent comments.

  • http://imanto.com Anto

    Told ya BANAGO, there is :P

    I know its not as easy, but it would help people. I remember how to do it now.

  • http://www.wplancer.com BANAGO

    @Anto: I am sure there is as I have implemented that at almost all the blogs I have designed, but what I meant was that it is not done on the way the archive is done. :)

  • http://imanto.com Anto

    Yea i know that :) I didnt say it was lol :P

    Anywhoo. Nuff said

  • http://www.booto.net KimCool

    good ways~!

  • Pingback: The Easy Way To Display Recent Posts in WordPress | Anidan Design : eco-friendly web & graphic design()

  • http://katasandi.com sandi

    I hope it can works in my blog

  • http://www.evenparsolutions.com Tony Oravet

    This is perfect! Exactly what I needed to show the most recent posts in a church website’s main page design. Thanks so much for the tip!

  • http://www.curtismchale.ca curtismchale

    How about a version where you exclude certain categories from the recent posts? this function doesn’t allow the exclusion of categories. I am using it inside the loop before the call for the actual post title in a lefthand sidebar and found that the other version of a custom database query fed the last post title into the main post the_title(). If I use the version you supplied then it doesn’t feed the wrong title to the post but I can’t exclude a few custom categories that feed my news and portfolio.

  • awesomeguy

    Thanks a bunch, worked beautifully!

  • http://www.ungoal.com toure

    eh thanks.
    what should I do to add a header to the lists?
    thanks again.

  • http://hyperbear.be Bjørn Endre

    Thanks! Just what I needed ;)

  • http://www.techieblogger.com Techie Blogger

    It’s just working fine for me. Thanks for this excellent hack :)

  • http://www.coreymade.com Corey

    Can you put time and date to this? If so how?

  • http://www.buylittlestpetshop.com Rick

    THANK YOU!!!! been wrestling with several recent post plugins but there was always something needing to be configured, edited, etc finally can remove all of the extra garbage needed to make them work

  • Leland

    @Corey: Not with this code, you would probably need to make a custom loop for that.

  • http://www.elliotcondon.com elliot condon

    clean and simple. Cheers for the code

  • Joyce

    How can I do this with categories ?

    • Leland

      You mean a list of categories or a list of posts from a certain categories?

      For a list of categories you could use wp_list_categories.

      For a list of posts from a certain category you would probably need a custom query. How to do that would need a new tutorial post entirely, but this should get you started.

  • http://electrojams.com/ Jordan Meeter

    Thanks, this helped me a ton. :)

  • Lightman

    I need to exclude posts from a certain category, how could be done?

    • Leland

      You’d need to make a custom loop and exclude the category. See the query_posts page for more info.

  • Mark

    Hi How do I make the list have the url’s inside Header tags?

    Thanks

  • http://pxlcr.com Gary David Hill

    Awesome bit of code! Only took me two years to find it. xD

    • Leland

      LOL…better late than never.

  • http://www.ubisanmanagement.co.uk colin

    How about displaying recent posts, together with a set number of characters from the actual entry?

  • Mark

    How do I do this and also have thumbnails? Very small thumbnails. 35px X 35 px. Google is annoying me sending me round in circles all day long. They’re useless lol.

    Well I await your useful reply.

    Thanks.

    • http://www.sourcesqr.com Kelvin

      Oh wow this is easy :D Thanks for this great article!

      BTW, has anyone come across functions to merge both recent post and page update in chronological order?

  • Leland

    Okay. All this code does is display a very simple list of recent posts. The markup is similar to this:

    <ul>
    <li><a href="http://example.com/recent-post-3">Latest One</a></li>
    <li><a href="http://example.com/recent-post-2">2nd Latest Post</a></li>
    <li><a href="http://example.com/recent-post-1">3rd Latest Post</a></li>
    </ul>

    If you want anything else (thumbnails, character counts, whatever) that would require a custom loop and I can’t really go over each specific case in the comments here.

  • Matthew

    Can you also tell the system to display the excerpt for the post? or just a teaser?

  • http://www.webdesign-bureau-of-mauritius.com/ Sachin @ Web Design Mauritius

    I’ve implemented it on my about page here: http://www.webdesign-bureau-of-mauritius.com/web-design-mauritius

    It works like a charm. Thanks for this!

  • http://www.dezignmatterz.com Mia

    Thanx Leland – just in the nick of time
    *Love ur site btw :D

    M.?

  • http://www.jivecomputer.com Andy

    Thanks! That solved my problem in 2 seconds.

  • http://blog.enladiana.com Sara

    Muchas gracias!!!! Thank you very much!!!! Just what I was looking for!

    Saludos desde Spain ;-)
    Sara

  • http://awildduck.com Ellery Davies

    I use the WordPress Twenty-Eleven theme and have always displayed recent posts using the widget in Jetpack. But I want this list to show in the individual posts as well. Is there a way to do this?