Quick tip on how to list recent posts in WordPress. This is possible thanks to a very handy function called wp_get_recent_posts().
WordPress Template File
Insert the following code in your theme file where you want to display the list of recent posts.
<h2>Recent Posts</h2>
<ul>
<?php
// arguments: show only 5 posts
$args = array( 'numberposts' => '5' );
// get the recent posts
$recent_posts = wp_get_recent_posts( $args );
// check if we have posts
if (!empty($recent_posts)) {
// loop trough posts array
foreach( $recent_posts as $post ) {
// echo each post on a list element
echo '<li><a href="' . get_permalink($post["ID"]) . '" title="Permalink To '.$post["post_title"].'" >' . $post["post_title"].'</a> </li> ';
}
}
?>
</ul>
Learn more about this function and its parameters on the codex.
Excellent i been looking for this, thank you
Thank you so much!!!
Thanks for the comments :)
Wasn’t actually looking for a post on this but it’s really going to help with my WordPress plug-ins.
Cheers Matt.
Thanks!! Exactly what I needed.
You got a truly helpful blog I’ve been right here reading for about an hour. I’m a newbie and your accomplishment is quite a lot an inspiration for me.