How to add post attachment image as featured image if no thumbnail is assigned or set a default fallback image in WordPress.

Featured image is assigned with post for various purposes in wordpress. If themes are designed to show mini thumbnail images next to recent posts, related posts, popular posts etc. you must assign featured image from the post editor panel unless no thumbnail will be shown. Featured images also give professional touch to content and create good impression to the readers.
Sometimes users forget to assign featured image while publishing their posts. Fallback of post thumbnail may breakdown the theme layout. If you are a theme designer, you should set a default fallback image to prevent breakdown of layout and design. There is another possible way to set a featured image automatically if any image is found as attachment with post.
Now I am going to show three options, first one is setting a default fallback image if no post thumbnail assigned, second option is automatically set featured image if post has any uploaded image attachment and third option is if no post thumbnail, no attachment found then display a default fallback image.
First option:
Checks if the post has a Post Thumbnail assigned to it and show it otherwise show default fallback image no_thumb.jpg. Illustrate an image and save it as ‘no_thumb.jpg’ then upload it into your theme image folder. Add this code inside your loop.
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail('small '); }else{ ?><img src="<?php bloginfo( 'template_directory' ); ?>/images/no_thumb.jpg" width=" " height=" " alt="No Image" /><?php } ?>
Second option:
Checks if the post has any image attachment and grab the first image from it. Copy and pest below code into theme functions.php
<?php //function to call first uploaded image from post as featured image function first_attachment_image($image_size) { $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => get_the_ID(), 'order' => 'ASC' ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { echo wp_get_attachment_image( $attachment->ID, $image_size ); } } } ?>
Copy and pest this code inside the loop where you want to the show thumbnail.
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail('small ');//Change small as your requirement. You can add custom image size if needed. }else{ if (function_exists('first_attachment_image')){ first_attachment_image('small');}} ?>
You may choose different image size. Simply change ‘small’ into ‘large’, ‘medium’ or ‘150, 150’.
Third option:
At first check post thumbnail, if not available, check post attachment, if no attachment image available then proceed to show default fallback image. Insert this code into functions.php
//function to call first uploaded image from post as default fallback featured image function first_attachment_image() { $args = array( 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1, 'post_status' => null, 'post_parent' => get_the_ID(), 'order' => 'ASC' ); $attachments = get_posts( $args ); if ( $attachments ) { foreach ( $attachments as $attachment ) { echo wp_get_attachment_image( $attachment->ID, '150, 150' ); } }else{ ?> <img src="<?php bloginfo('template_directory') ?>/images/no_thumb_related.jpg" alt="No thumbnail picture" width="150" height="80" /> <?php } }
Copy and pest this code inside the loop where you want show thumbnail
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it. the_post_thumbnail('small ');//Change small as your requirement. You can add custom image size if needed. }else{ if (function_exists('first_attachment_image')){ first_attachment_image();}} ?>
Was this tutorial helpful? Share it with your friends on Twitter, Facebook, Linkedin & Google!