Recent posts with featured image or fallback image with permalink

Here’s the relevant part of your code that should work:

// This will make a URL like http://yoursite.com/path/to/fallback.png
$fallback_image = site_url( '/path/to/fallback.png' ); 
$fallback_image = "<img src="https://wordpress.stackexchange.com/questions/224740/{$fallback_image}" />";

foreach( $recent_posts as $recent ){        
    echo '<div class="sidebar-entries">';
    $featured_image = get_the_post_thumbnail( $recent['ID'], 'sidebar-thumb', array( 'class' => 'sidebar-image' ) );

    if ( ! strlen( $featured_image ) ) {
        $featured_image = $fallback_image;
    }

    $permalink = '<a href="' . get_permalink( $recent['ID'] ) . '">%s</a>';

    echo sprintf( $permalink, $featured_image );

    echo '<div class="sidebar-entries-title">';
    echo sprintf( $permalink, __( $recent["post_title"] ) );
    echo '<div class="sidebar-date">';

    $timestamp = '<p>' . human_time_diff( strtotime( $recent['post_date'] ), current_time('timestamp') ) . ' ago </p>';
    echo sprintf( $permalink, $timestamp );
    echo '</div>';
    echo '</div>';
    echo '</div>';
}

The basic idea is

  • Store your fallback image in a variable outside of your loop
  • Get the featured image in the loop and store it in a variable
  • If you get an empty string ( ! strlen() )
    • Replace the contents of your featured image variable with the fallback image variable

EDIT: Sorry, forgot about the permalink part. This approach uses sprintf to make your permalink code consistent.