Skip to content

Instantly share code, notes, and snippets.

@chrillo
Created November 29, 2011 08:08
Show Gist options
  • Save chrillo/1403955 to your computer and use it in GitHub Desktop.
Save chrillo/1403955 to your computer and use it in GitHub Desktop.
Get recent posts from Wordpress
// include in functions.php
function get_recent_posts($num=3){
$postsQuery = new WP_Query();
$args=array(
'showposts='=> $num,
'orderby'=>'posted',
'post_type'=>'post',
//'taxonomy'=>'category', uncomment to filter posts within a category
// 'term'=>'news', e.g.: news - could be anything
);
$posts=$postsQuery->query($args);
$html="<ul class='recent-posts'>";
foreach($posts as $post){
$html.="<li><a href='".get_permalink($post->ID)."'>".$post->post_title."</a></li>";
}
$html.="</ul>";
return $html;
}
// usage in template
echo get_recent_posts(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment