Skip to content

Instantly share code, notes, and snippets.

@paulruescher
Created June 26, 2012 19:02
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • 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; ?>
<ul>
<?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; ?>
</ul>
<?php
echo $after_widget;
wp_reset_postdata();
endif;
}
}
function my_recent_widget_registration() {
unregister_widget('WP_Widget_Recent_Posts');
register_widget('My_Recent_Posts_Widget');
}
add_action('widgets_init', 'my_recent_widget_registration');
@patrick-wc
Copy link

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

@zionandzion
Copy link

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

@ThomasBck
Copy link

Thanks for this trick ! it works like a charm

@brunopontes-hotmart
Copy link

Awesome! Thank you!

@socrec
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

@alissoncs
Copy link

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

@jozefini
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...

@EastSideCode
Copy link

Great stuff man, thanks!

@krm218
Copy link

krm218 commented Apr 7, 2017

Thank you for this! Great!

@kjy112
Copy link

kjy112 commented Aug 20, 2017

This is great. Thank you for this!

@markandcurry
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!

@mobeigi
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.

@redwolfmendoza
Copy link

Dude! Totally worked! Thanks man.

@anwer-456
Copy link

Works like a Charm.Thanks a Million Paulruescher.

@isairuiz
Copy link

isairuiz commented Oct 4, 2018

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

@braddalton
Copy link

You could also use the widget_posts_args filter

@aramossanchez
Copy link

love you so much

@NikAndreev
Copy link

I love you!!

@CordialGit
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?

Regards

@braddalton
Copy link

You can use this code and add a query arg for categories using WP_Query https://developer.wordpress.org/reference/classes/wp_query/#category-parameters

@CordialGit
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-- https://prntscr.com/1t7t51w

to something looking like this-- https://prntscr.com/1t7t750 or like this-- https://prntscr.com/1t7rtk0 -- with Category filter.

When the PHP Code has been added, the settings Page should look something like this-- https://prntscr.com/1t7tql4

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

Regards.

@braddalton
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