14

I'd like to enable the Recent posts widget with the ability to show a "View All" link when there are more posts than I've currently got the widget set to show.

I'd also like to be able to exclude certain categories or posts from it.

Is it possible to do either via my theme's functions.php?

(1) Customize the existing widget or (2) Create a copy of it that I can customize?

1
  • 1
    For further reading, the Widgets API is a helpful resource.
    – manafire
    Nov 9, 2012 at 21:48

1 Answer 1

22

It's usually best to copy the existing one, name it something unique, and then add your own functionality.

Copy the WP_Widget_Recent_Posts class from wp-includes/class-wp-widget-recent-posts.php into your functions.php (or, preferably, another file in your theme devoted just to widgets) and rename to something else, like My_Widget_Recent_Posts

Add your functionality in there.

Don't forget to call register_widget('My_Widget_Recent_Posts') as part of the init phase as well.

3
  • +1 Sweet. Ok, I added it to a special custom widgets file I have that's "included" via "require_once()" in my functions.php. However, although I changed the classname, it appears to take over the default WP Recent Posts widget. Any ideas?
    – N2Mystic
    Oct 13, 2011 at 20:38
  • 2
    You must assign a unique name to the widget. parent::__construct('recent-posts', __('Recent Posts'), $widget_ops); 'recent-posts' should be changed to something like 'my-recent-posts' , and you should also change the label to something that makes the widget identifiable as yours: 'My Recent Posts'
    – Bendoh
    Oct 13, 2011 at 20:43
  • 3
    To init my widget I had to add add_action( 'widgets_init', create_function( '', 'register_widget( "My_Widget_Recent_Posts" );' ) ); to functions.php.
    – manafire
    Nov 9, 2012 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.