What's WP
Our everything is about WordPress - best choice of WordPress users!
How to Set Default Fallback Image for Post Thumbnails in WordPress

How to Set Default Fallback Image for Post Thumbnails in WordPress

WordPress comes with tremendous numbers of fascinating features during its updates. With the launch of WordPress version 2.9, the post thumbnail feature has been added to the list of its offerings. This feature facilitates in enhancing your post by adding a custom featured image that represents your content.

Generally, people prefer to upload a unique featured image for each post, but some others prefer to set the fallback images for post thumbnails. In this article, we’d like to discuss the steps to assign a specific image or assign the first post image as the default fallback image.

Add a Specific Image as the Default Fallback Image

Although thousands of webmasters opt for unique post thumbnail option in their blogs and websites, some newbies may forget to add a custom image in the thumbnail for each post. If this happens, the featured image section of a post looks unprofessional, creating a negative impact on the prospective viewers and drops down the reputation of your site.

For this reason, if you want to keep up with the thumbnail feature all the times, it is recommendable to set a custom image as the default fallback image. This means that when you forget to attach a featured image to a post, the default fallback image will automatically be set as the thumbnail.

To start with, you should log into your WordPress dashboard using the admin login details. Through the dashboard, you have to navigate to the theme files including single.php, loop.php, archive.php, home.php and index.php. Select one and insert the following code. Note that you can also do this via the File Manager of your control panel.

<?php 
if ( has_post_thumbnail() ) 
{
    the_post_thumbnail();
}
else 
{ 
?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-thumb-img.png" alt="<?php the_title(); ?>" />
<?php } ?>

After inserting this code, you can go back and refresh your website to check whether there is a specific thumbnail image displayed in a post that lacks a custom featured image. If your chosen image is detected, then it appears every time when you forget to upload the thumbnail image for your blog post.

It should be noted that the code mentioned above adds just a basic default fallback image as the post thumbnail. You can modify and enhance it by configuring its sizes and classes.

Set the First Post Image as a Default Fallback Image

With the increasing SEO guidelines and readers’ expectations, a majority of posts involve the presence of content excerpts as well as the first post images. This is a great trick used to increasing the reading experience of visitors and the indexing process of searching spiders. Also, it can be adopted in the settings of thumbnail image option.

Here, it means that you can automatically assign the first post image as the post thumbnail image if you forget to assign one. To do this, you should dig into the functions.php file of your activated theme. Note that the file has to be opened and edited using an advanced text editor. Then, you need to add the following code.

//function to call first uploaded image in functions file
function main_image() 
{
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image&order=desc');
    if($files) :
        $keys = array_reverse(array_keys($files));
        $j=0;
        $num = $keys[$j];
        $image=wp_get_attachment_image($num, 'large', true);
        $imagepieces = explode('"', $image);
        $imagepath = $imagepieces[1];
        $main=wp_get_attachment_url($num);
        $template=get_template_directory();
        $the_title=get_the_title();
        print "<img src='$main' alt='$the_title' class='frame' />";
      endif;
}

After inserting the code into the functions.php file, don’t forget to save the changes. Now, you must navigate to the theme files that you are using currently, and enter the following shortcode.

<?php 
if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail())) 
{
    echo get_the_post_thumbnail($post->ID);
} 
else
{
    echo main_image();
} ?>

Save the file to implement the changes. Now, you can notice that all the posts that lack the featured images will set the first image as the post thumbnail.

Here, we have to mention that this method may fail to bring you a perfect effect. After all, you cannot make sure that the size of the first image added for each post 100% suits your thumbnail settings. And this may cause a compromise of image quality.

Recommended Plugins for Fallback Images

If you do not want to play with the codes, WordPress always allows you to get your job done with the help of a plugin. Hence, you can use the Default Featured Image plugin, which is an efficient tool for executing this task.

To configure the plugin, you should firstly install and activate it on your WordPress site. Then, you can find that a new option has been added to the Media tab of the Settings menu. The newly added option for attaching the featured image allows you to upload an image and to set it as the default fallback image.

Default Featured Image plugin

For this reason, you should browse through your desktop and choose a relevant image. Upon the selection, you must upload the image and save the settings. Then, you can find that the chosen image can be displayed as the post thumbnail when there is no featured image uploaded on your posts.

Apart from the Default Featured Image plugin, you can use some alternative tools such as WordPress Auto Featured Image and Default Thumbnail Plus plugin. It should be noted that the configuration procedures of these plugins are simply same as discussed above.

Author

Joyce is a professional writer & SEOer who loves trying new things and sharing the experience through blogging. She loves clean design, playing with different WordPress themes, SEO, etc. Now she is very struggle with purchasing a bubbling apartment in downtown. You can help her by clicking our ADs or making a purchase via our links ;-)

Close

Want to Learn More About WordPress?

Get Our Free Themes, Plugins, Support, SEO Tips, and Other Tutorials!

Email *