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:


[recent-posts]

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

Recent Post Short Code In WordPress

1

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>';

endwhile;

wp_reset_query();

return $list . '</ul>';

}

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

2

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


[recent-posts]

3

Ta-Da!

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

7 Comments

  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() . '' . '';
    endwhile;
    wp_reset_query();
    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
    M

    • Hi Marco,

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

  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.

This post currently has 7 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 *