Created
November 29, 2011 08:08
-
-
Save chrillo/1403955 to your computer and use it in GitHub Desktop.
Get recent posts from Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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