List recent posts in WordPress


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.

Entry by | Original code is licensed under the GNU GPLv2 license.

Reactions (6)

  1. 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.