Here’s one way to do it through the the_title
filter. We can limit the scope to the Recent Posts widget, by initialize it within the widget_posts_args
filter and then remove it again after the loop.
/**
* Recent Posts Widget: Append Thumbs
*/
add_filter( 'widget_posts_args', function( array $args )
{
add_filter( 'the_title', 'wpse_prepend_thumbnail', 10, 2 );
add_action( 'loop_end', 'wpse_clean_up' );
return $args;
} );
where we define
function wpse_prepend_thumbnail( $title, $post_id )
{
static $instance = 0;
// Append thumbnail every second time (odd)
if( 1 === $instance++ % 2 && has_post_thumbnail( $post_id ) )
$title = get_the_post_thumbnail( $post_id ) . $title;
return $title;
}
and
function wpse_clean_up( \WP_Query $q )
{
remove_filter( current_filter(), __FUNCTION__ );
remove_filter( 'the_title', 'wpse_add_thumnail', 10 );
}
Note that because of this check in the WP_Widget_Recent_Posts::widget()
method:
get_the_title() ? the_title() : the_ID()
the the_title
filter is applied two times for each item. That’s why we only apply the thumbnail appending for the odd cases.
Also note that this approach assumes non empty titles.
Otherwise it’s more flexible to just create/extend a new widget to our needs instead.