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

This comment has been minimized.

Copy link

@patrick-wc patrick-wc commented Mar 10, 2014

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

@zionandzion

This comment has been minimized.

Copy link

@zionandzion zionandzion commented Jan 19, 2015

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

@ThomasBck

This comment has been minimized.

Copy link

@ThomasBck ThomasBck commented May 12, 2015

Thanks for this trick ! it works like a charm

@brunopontes-hotmart

This comment has been minimized.

Copy link

@brunopontes-hotmart brunopontes-hotmart commented Apr 5, 2016

Awesome! Thank you!

@socrec

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@alissoncs alissoncs commented Jul 22, 2016

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

@CodeAlb

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@EastSideCode EastSideCode commented Jan 4, 2017

Great stuff man, thanks!

@krm218

This comment has been minimized.

Copy link

@krm218 krm218 commented Apr 7, 2017

Thank you for this! Great!

@kjy112

This comment has been minimized.

Copy link

@kjy112 kjy112 commented Aug 20, 2017

This is great. Thank you for this!

@markandcurry

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@redwolfmendoza redwolfmendoza commented May 10, 2018

Dude! Totally worked! Thanks man.

@anwer-456

This comment has been minimized.

Copy link

@anwer-456 anwer-456 commented Sep 18, 2018

Works like a Charm.Thanks a Million Paulruescher.

@isairuiz

This comment has been minimized.

Copy link

@isairuiz isairuiz commented Oct 4, 2018

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

@braddalton

This comment has been minimized.

Copy link

@braddalton braddalton commented May 25, 2019

You could also use the widget_posts_args filter

@aramossanchez

This comment has been minimized.

Copy link

@aramossanchez aramossanchez commented Mar 1, 2020

love you so much

@NikAndreev

This comment has been minimized.

Copy link

@NikAndreev NikAndreev commented Dec 25, 2020

I love you!!

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