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.