Recent Post Shortcode

WordPress shortcodes are a simple way to set up functions to create macro codes for use in post content. For instance, the following shortcode (in the post/page content) would add your recent posts into the page:


It’s pretty simple and brings your WordPress blog alive with ease.

Recent Post Short Code In WordPress


Add this code to your functions.php file.

function my_recent_posts_shortcode($atts){
 $q = new WP_Query(
   array( 'orderby' => 'date', 'posts_per_page' => '4')

$list = '<ul class="recent-posts">';

while($q->have_posts()) : $q->the_post();

 $list .= '<li>' . get_the_date() . '<a href="' . get_permalink() . '">' . get_the_title() . '</a>' . '<br />' . get_the_excerpt() . '</li>';



return $list . '</ul>';


add_shortcode('recent-posts', 'my_recent_posts_shortcode');


Add this shortcode to the page where you would like to pull in your recent posts.




To find out more about WordPress shortcodes, check out the Shortcode API Codex.

Want to become a better web developer?

Join over 25,000 other developer & designers who get awesome links to the best news and articles each week delivered directly to their inbox.


  1. Hi Jake

    Great tutorial! How can I incorporate the post thumbnail? I’m using the following code below which works great, only it includes the full post image as opposed to my 200px x 200px thumbnail. I checked the link to WordPress that you posted but couldn’t find any information for this.

    function my_recent_posts_shortcode($atts){
    $q = new WP_Query(array( 'orderby' => 'date', 'posts_per_page' => '2'));
    $list = '';
    while($q->have_posts()) : $q->the_post();
    $list .= '' . get_the_post_thumbnail('thumbnail') . '' . get_the_title() . '' . '' . get_the_date() . '' . '' . get_the_excerpt() . '' . '';
    return $list . '';
    add_shortcode('recent-posts', 'my_recent_posts_shortcode');

    • Hey Gemma,

      Have you enabled support for post thumbnails in your functions.php file?

      If not, this page explains it – WP Thumbnail Codex.

      Hope that helps.

  2. Thanks for this – after messing about for hours I finally have what I want thanks to your clear instructions!

  3. Hi, thank you about yout code!
    I’ve a small problem, the text are by one line and it’s very long on right,
    how to format?

    thank you,
    have a nice day

    • Hi Marco,

      Feel free to provide a screenshot so I can understand your problem.

    • how to display the image in the post using this shortcode ????
      what is the function to display the image ….

  4. Thanks for the tutorial!

    Any idea how to list recent posts but only from one category please?

  5. Hi Lewis,

    If you change the query to include the category all should be good.

    Something like this:
    $q = new WP_Query(
    array( 'orderby' => 'date', 'posts_per_page' => '4', 'cat' => '9')

    Note that cat' => '9' is added.

    Where “9” is your desired category.

    To find the correct category. either go to the category, it will be displayed at the end of the URL. Or, go to Manage categories and hover your mouse over a category. The cat id will show up.

  6. Do you happen to have something for images?
    I’d love to put a 50px X 50px (max) image to the left top and a little less content. Maybe limit the excerpt to 50 characters.

    • Hey eDD,

      All you need to do it adjust the output of the list item to include the post thumbnail and then limit the excerpt.

      This article on the WordPress Codex should help you with the post thumbnail and this one in limiting the excerpt.

      I hope that helps.

  7. I’m a complete WP novice, but I can copy and paste! Where exactly in the functions.php file do I post the code? I posted it at the end of the code, but that did not work. Thanks.

  8. Thanks a lot for this post. However is there a way ican change the colour of the titles in my recent posts.

    • Please ignore, managed to get it.

    • just put this code for get title
      ‘ . get_the_title() . ‘

  9. Just found this and it’s great! Thanks so much!

  10. Hi Jake.

    Thanks for the code. Before i include this code i want to know if it will apply for home page. I need all listed properties to be listed as recent properties.

    Thank you in advance,

  11. I was looking for this solution for a long time, and here it is, finally! Thank you so much! However, is there a way to add a read more tag that’s leading to the full blogpost? I can’t figure it out yet.

    Thanks again!

  12. Hello sir

    How can i display recent posts with category and how can i show there tags like date or name?


  13. Hi Sir,

    I wanna display recent posts in my blog body of the content, But am using this short codes but not displayed, Please help to display posts

This post currently has 19 responses. What do you think?

You can use basic HTML when posting code, please turn all < characters into &lt; or > into &gt;
If the code is multi-line, use <pre><code></code></pre>

Leave a Reply

Your email address will not be published. Required fields are marked *