Gordonmac Dot Com

Mostly a web development blog

Displaying recent posts in the WordPress sidebar

Posted: November 14th, 2011 | Tags: | Posted in: PHP, Tutorials, Wordpress
Note: This tutorial was originally published in 2011. The tips and techniques explained may be outdated.

To add recent posts navigation to your WordPress blog, simple add the following code to sidebar.php.

<h2>Recent Posts</h2>
<nav id="navigation-recent">
<ul>
<?php
$recent_posts = wp_get_recent_posts(5);
foreach( $recent_posts as $recent ){
	$date = date("M d, Y",strtotime($recent["post_date"]));
	echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' .   $recent["post_title"].'</a> '.$date.'</li>'; 
}
?>
</ul>
</nav>

This will display the 5 most recent posts. To change the number of recent posts just edit the following:

$recent_posts = wp_get_recent_posts(10);

This will now display the 10 most recent posts.