Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2012 19:02
Show Gist options
  • Save paulruescher/2998060 to your computer and use it in GitHub Desktop.
Save paulruescher/2998060 to your computer and use it in GitHub Desktop.
Used this to change the output of WordPress' Recent Posts Widget
* Extend Recent Posts Widget
* Adds different formatting to the default WordPress Recent Posts Widget
Class My_Recent_Posts_Widget extends WP_Widget_Recent_Posts {
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
if( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
$number = 10;
$r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );
if( $r->have_posts() ) :
echo $before_widget;
if( $title ) echo $before_title . $title . $after_title; ?>
<?php while( $r->have_posts() ) : $r->the_post(); ?>
<li><?php the_time( 'F d'); ?> - <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
echo $after_widget;
function my_recent_widget_registration() {
add_action('widgets_init', 'my_recent_widget_registration');
Copy link

Thanks, I've seen it copied and pasted from core before but not with extends, nice.

Copy link

Awesome! This + Timber plugins Timber::compile() method is a dream!

Copy link

Thanks for this trick ! it works like a charm

Copy link

Awesome! Thank you!

Copy link

socrec commented Apr 13, 2016

I don't understand where exactly i'm going to those lines of code into? Please help me out, thank you

Copy link

@socrec you can put this code inside "functions.php" file

Copy link

I have been looking for this long time ago, thank you for sharing...
Why when i change the code like this doesn't work:

// as default it is $show_date

If i wan't to disable the time from costumizer but it disappear even if it is active...

Copy link

Great stuff man, thanks!

Copy link

krm218 commented Apr 7, 2017

Thank you for this! Great!

Copy link

kjy112 commented Aug 20, 2017

This is great. Thank you for this!

Copy link

I was looking everywhere for a place to drop the excerpt beneath the post, but didn't want to do it with a plugin. This is exactly what I needed. Thanks Paul!

Copy link

mobeigi commented Apr 15, 2018

In my opinion its probably just better to create your own copy of the class and modify it to suit your needs. I made a top posts widget which just extends the base WP_Widget.

Copy link

Dude! Totally worked! Thanks man.

Copy link

Works like a Charm.Thanks a Million Paulruescher.

Copy link

isairuiz commented Oct 4, 2018

What file needs to be change with this code? thanks.

Copy link

You could also use the widget_posts_args filter

Copy link

love you so much

Copy link

I love you!!

Copy link

What I am looking for, is to extend the default WordPress "Recent Post Widget". All I really want to add is ability to filter which particular Category I want.

Which particular code should I use?


Copy link

You can use this code and add a query arg for categories using WP_Query

Copy link

@ braddalton

First and foremost, Thank You very much for your reply.

Then going forward, I'd like to say that there's a big challenge with your answer, and the problem is actually me.

The problem is me because I don't know PHP at all, and can't write the first line of PHP Code.

However, I pick up Code Snippets here and there, and dabble into the functions.php file of my Child Theme to add actions and filter hooks. That's how far I can do for now.

So your answer really didn't help me, but sent me into a deeper abyss and vortex of confusion: I meant that I did go to the WP_Query Page, but don't know what exactly I need to take out from there, to achieve what I need.

What I need is to enhance the default WordPress "Recent Posts" Widget with having Thumbnail image pulled from Featured Image, then give it ability to filter Post Category, Tags, Author, Excerpt, Read More and so on.

I am looking at having the default WordPress "Recent Posts" Widget move from something looking like this--

to something looking like this-- or like this-- -- with Category filter.

When the PHP Code has been added, the settings Page should look something like this--

The Challenge and Question is--- Which PHP Code or PHP Code Snippets help me achieve something like that, without using a plugin?


Copy link

If you don't know coding i suggest you find a free plugin which includes the options/settings you need. Otherwise, you could hire someone to modify the widget for you.

This is not the place to come and get someone to write you custom code.

Note : This code is approx 8 years old and would need updating first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment