Skip to content

Instantly share code, notes, and snippets.

@mentholiptuz
Last active December 22, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mentholiptuz/6482150 to your computer and use it in GitHub Desktop.
Save mentholiptuz/6482150 to your computer and use it in GitHub Desktop.
Add this shortcode to the page where you would like to pull in your recent posts. Shortcode: [recent-posts]
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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment