How To Set Default Thumbnail Image In WordPress ?

Do you want to set the default thumbnail/featured image in WordPress?

The term ‘default thumbnail image’ here refers to the image that is applied as the featured image when the post does not have a certain image set as a thumbnail.

There are two best ways to do this.

In the first one, you can set a custom image as the default featured image by URL and thumbnail ID.

And in the second you set the first image from the post as a thumbnail.

How to set Default Thumbnail image in wordpress ?

So let’s see the first way by which we can set default thumbnail image by both URL and Image ID –

Set default thumbnail image by custom image url.

Simply paste the below code into functions.php or the Code Snippet Inserter plugin.

Replace the given image URL with your default featured image URL and it’s done.

// Set The Default Thumbnail Image 
function smp_post_thumbnails_html( $html ) {
    if ( $html == '' ) { // If No Html Img Tags Found
$image = esc_url(get_stylesheet_directory_uri().'/images/thumbnail.png'); // Add Default Thumbnail Image URL
$alt = get_the_title(); // Get Post Title As Alt Tag
        $html = "<img class='thumbnail' src='$image' alt='$alt'/>"; // Create The Img Tag
    }
    return $html;
}
add_filter( 'post_thumbnail_html', 'smp_post_thumbnails_html' );
add_filter( 'has_post_thumbnail', '__return_true' );

Set default thumbnail image by image id

You can also set a default featured image by Image ID.

But need to make sure that you have uploaded the desired image via WordPress (which means it should be in the /wp-content folder and should be visible in Wp Media Library).

Otherwise, you can’t know about the ID of the image. Because you get the ID of the image only through WordPress.

How to check image id in WordPress ?

« Previous
Next »

Passionate About Writing Code, Murari Poonia is a Student cum Web Developer cum Part Time Blogger. Who is Always Eager to Hunt New Ideas.

Comments(0)

See Comments

    Leave a Comment