-
-
Save paulruescher/2998060 to your computer and use it in GitHub Desktop.
/** | |
* 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'); |
Thanks for this trick ! it works like a charm
Awesome! Thank you!
I don't understand where exactly i'm going to those lines of code into? Please help me out, thank you
@socrec you can put this code inside "functions.php" file
I have been looking for this long time ago, thank you for sharing...
Why when i change the code like this doesn't work:
If i wan't to disable the time from costumizer but it disappear even if it is active...
Great stuff man, thanks!
Thank you for this! Great!
This is great. Thank you for this!
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!
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
.
Dude! Totally worked! Thanks man.
Works like a Charm.Thanks a Million Paulruescher.
What file needs to be change with this code? thanks.
You could also use the widget_posts_args filter
love you so much
I love you!!
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
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
@ 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.
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.
Awesome! This + Timber plugins Timber::compile() method is a dream!