Wordpress widgets, playing with recent posts

This morning I wanted to display the most recent post from the blog in the footer, don’t ask me why I just wanted to.

I know one thing or two about php and I thought it would be straight forward. Locate the function from the sidebar.php, copy and paste it, done.

Well not so easy. First I didn’t find any recent posts function in the sidebar and I should have expected that, because of the way the widgets work. The sidebar don’t run them one by one but run the dynamic_sidebar function which build the widget needed.

Next step, looking into the widget file to see what’s in there. You will find the file in includes/widgets.php. Do a research for “recent” and you will bump into the wp_widget_recent_entries function, around line 860.

function wp_widget_recent_entries($args) {
if ( $output = wp_cache_get('widget_recent_entries', 'widget') )
return print($output);

ob_start();
extract($args);
$options = get_option(’widget_recent_entries’);
$title = empty($options['title']) ? __(’Recent Posts’) : $options['title'];
if ( !$number = (int) $options['number'] )
$number = 10;
else if ( $number < 1 )
$number = 1;
else if ( $number > 15 )
$number = 15;

$r = new WP_Query(”showposts=$number&what_to_show=posts&nopaging=0&post_status=publish”);
if ($r->have_posts()) :
?>
<?php echo $before_widget; ?>
<?php echo $before_title . $title . $after_title; ?>
<ul>
<?php while ($r->have_posts()) : $r->the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php if ( get_the_title() ) the_title(); else the_ID(); ?> </a></li>
<?php endwhile; ?>
</ul>
<?php echo $after_widget; ?>
<?php
wp_reset_query(); // Restore global post data stomped by the_post().
endif;
wp_cache_add(’widget_recent_entries’, ob_get_flush(), ‘widget’);
}

This is what we want to run to display the most recent posts or entries of the blog. If you try to call the function like this:

<?php wp_widget_recent_entries('5') ?>

Where 5 is the number of post you want to display you will get an error saying :

extract() [function.extract]: First argument should be an array

Let’s go back to the function. This extract function is here to get keys and values from an array, which should be passed when calling the function. After running extract, each value in the array is stored in a variable with the same name as the key of the value.

Go down a bit in the function. See $number, $before_widget, $before_title, etc…? These are values from the array, extracted in different variable. Good, let’s build an array that has all this:

$recentParam = array('number'=>'5', 'before_title'=>'<h4>', 'after_title'=>'</h4>', 'before_widget'=>'<div>', 'after_widget'=>'</div>');

And know call the function, passing your array:

$recentParam = array('number'=>'5', 'before_title'=>'<h4>', 'after_title'=>'</h4>', 'before_widget'=>'<div>', 'after_widget'=>'</div>');
wp_widget_recent_entries($recentParam);

That should display your 5 latest posts in a div where you have called the function. However you need to remove the recent post widget from your sidebar in the widget admin page if you have it.

4 Responses to “Wordpress widgets, playing with recent posts”

  1. Kishore

    I just moved from blogger to my own site and wanted to put recent posts on 404 page as all my permalinks are dead. That wasn’t that easy at all.. this post was a lifesaver….

  2. Ben

    Hi Kishore, glad it was of help :)

    There is also another possibility to do this: if you create a file for the 404 page in your theme and widgetize it you can use the recent post widget to display recent post in it.

    I might write an article about that soon. Good idea to display recent posts in the 404 by the way.

  3. haden

    This was just what i was looking for.. thanks alot..

  4. Widgetise your template for wordpress | Your Site Is Valid

    [...] had a few comments on the article about displaying recent post where you want. The solution I gave in the post work but I’d like to present here anĀ  alternative solution [...]

Leave a reply