Skip to content

Instantly share code, notes, and snippets.

@zenman
Created February 19, 2014 18:28
Show Gist options
  • Save zenman/9098343 to your computer and use it in GitHub Desktop.
Save zenman/9098343 to your computer and use it in GitHub Desktop.
Recent Posts Shortcode
<?php
/*
* Add recent posts shortcode. Use like so: [recent-posts posts='5' slug='uncategorized']
*/
function recent_posts_function($atts){
extract(shortcode_atts(array(
'posts' => 5,
'slug' => 'uncategorized'
), $atts));
$return_string = '<ul>';
query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts , 'category_name' => $slug));
if (have_posts()) :
while (have_posts()) : the_post();
$return_string .= '<h4><a href="'.get_permalink().'">'.get_the_title().'</a></h4>';
$return_string .= '<p>'.get_the_excerpt().'</p>';
endwhile;
endif;
$return_string .= '</ul>';
wp_reset_query();
return $return_string;
}
add_shortcode('recent-posts', 'recent_posts_function');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment