How can i change the recent post widget title programmatically?
I use this code in my footer.php to add recent posts widget.
<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI had a similar situation. Add the following code to your functions.php and see if it works for you.
function my_widget_title($title, $instance, $id_base) {
if ( 'recent-posts' == $id_base) {
return __('New Title');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Note: this will change the title of all the "Recent Post" widgets.
One of the ways is:
1) Rename the Widget class name
2) Modify the Widget class
3) Register your Widget:
function wpse97413_register_custom_widgets() {
register_widget( 'wpse97411_Widget_Recent_Posts' );
}
add_action( 'widgets_init', 'wpse97413_register_custom_widgets' );
Just for the sake of completion:
As you can see here you can pass the widget multitude of things straight in the code too. Let's say you want the widget to show three posts, with custom title and you want to wrap the title in h2+span tags for styling purposes you could do this:
the_widget( 'WP_Widget_Recent_Posts',
array ( 'title' => 'Three of dem posts', 'number' => 3 ),
array ( 'before_title' => '<h2 class="block-title"><span>', 'after_title' => '</span></h2>' )
);
Which would produce just that.
You can also of course just pass only the 'title', which would give the default amount of 5 posts on the widget.