09 February 2013

WordPress: More customizable ways of displaying recent posts

When using the WordPress widgets to display the most recent posts, the level of customization is somewhat reduced, provided the user does not want to alter the way WordPress displays the resulting lists.

The following code snippets show how to display the most recent posts in WordPress, to be used on either the sidebar or footer sections of a theme, in the case you do not use the WordPress widgets. These allow customization in a somewhat easy way.

This code will display the 10 most recent posts, from newest to oldest, in a vertical list format.

$args = array( 'numberposts' => '10' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
	echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' . $recent["post_title"].'</a></li>';

The next code snippet displays the 10 most recent posts with the date before the entry title, from newest to oldest, in a list format:

<?php $postslist = get_posts('numberposts=10&offset=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>

<li><?php the_time('d.m.y') ?>  <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>
<?php endforeach; ?>

And here you go, two examples of displaying recent posts in another way.

Last updated on
How-To, WordPress

Related Articles

Responses to the article

Wednesday, 13th February 2013

Thanks, this is great. Why is this code not available at the codex of wordpress? Is this any bad hack or anything? Works very nice anyway!

Monday, 30th December 2013

Robbert: These code snippes are put together with the help of the WordPress documentation (codex), available at WordPress.org (http://codex.wordpress.org/Main_Page) and several other online sources. Not bad hacking here.

You could also use the last code snippet to set up a grid system type of layout (magazine like) or like I display excerpts on this blog, with a little bit of modifications.

Leave a response

Fields marked with * are required. Email addresses are never shared.