Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

patrick-wc commented Mar 10, 2014

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

@zionandzion
Copy link

zionandzion commented Jan 19, 2015

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

@ThomasBck
Copy link

ThomasBck commented May 12, 2015

Thanks for this trick ! it works like a charm

@brunopontes-hotmart
Copy link

brunopontes-hotmart commented Apr 5, 2016

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

alissoncs commented Jul 22, 2016

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

@iAmJozi
Copy link

iAmJozi commented Aug 23, 2016

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

EastSideCode commented Jan 4, 2017

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

markandcurry commented Sep 21, 2017

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

redwolfmendoza commented May 10, 2018

Dude! Totally worked! Thanks man.

@anwer-456
Copy link

anwer-456 commented Sep 18, 2018

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

braddalton commented May 25, 2019

You could also use the widget_posts_args filter

@aramossanchez
Copy link

aramossanchez commented Mar 1, 2020

love you so much

@NikAndreev
Copy link

NikAndreev commented Dec 25, 2020

I love you!!

@CordialGit
Copy link

CordialGit commented Sep 22, 2021

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

braddalton commented Sep 22, 2021

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

CordialGit commented Sep 22, 2021

@ 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

braddalton commented Sep 22, 2021

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