Soliloquy Blog

WordPress slider tips, Tutorials and Resources.
How to Set a Default Featured Image in WordPress

How to Set a Default Featured Image in WordPress

by Waseem Abbas on August 22, 2016

Do you want to display a default featured image for your WordPress posts? You may want to have a fallback in case you forget to add one. In this article, we will share how to set a default featured image in WordPress.

Adding a default featured image in WordPress will ensure that your blog posts look professional in your theme and when shared on social media, even if you forget to add a featured image.

There are three different methods you can use to set a default featured image in WordPress:

  • Method 1: Set a branded image as the fallback featured image
  • Method 2: Set the first post image as the default featured image
  • Method 3: Use the WordPress Default Featured Image plugin

Ready to get started? Let’s go!

Method 1: Setting up a Branded Image as Fallback Featured Image

Sometimes you have a blog post with no images and no featured image, and you want a fallback image to be displayed as the featured image for this post.

First, open the theme file where you want to add the fallback featured image. It can be any theme file like home.php, single.php, index.php, and more.

Then, in the post loop, you need to paste the following code:


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

This code will verify that there is a featured image assigned to your blog post. If there is a featured image available, then that will be displayed. Otherwise, it will display default-image.jpg from the images folder of your theme.

Method 2: Setting up the First Post Image as Default Featured Image

Sometimes you may want to use the first image within the post as the featured image. Here’s how to do it automatically…

First, you need to add the following code to your theme’s function.php file:


//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;
}

Next, you need to open the theme file where you want to display the featured image. Simply add the following code:


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

Now your post will use the first image as the featured image. However, if a featured image exists, then it will display that instead.

Method 3: Using the WordPress Default Featured Image Plugin

If you are not familiar with code or want to go easy way, then you can use the WordPress Default Featured Image plugin.

First thing you need to do is install and activate Default Featured Image plugin. (Check out this guide to learn how to install a WordPress plugin)

Upon activating the plugin, you need to go to Settings ยป Media and click on the Select Default Featured Image button.

Set Default Featured Image

Select an image from your computer and make sure to save the changes.

This plugin will check to see if there’s a featured image available for your post, and if so, that will be displayed. But if there is no featured image available, then your uploaded image will be displayed as the default featured image.

We hope this article has helped you to learn how to set a default featured image in WordPress. You may also want to check out our guide on how to add a WordPress favicon to your site.

If you like this article, then please follow us on Facebook and Twitter for more free WordPress tutorials.

Waseem Abbas is a content writer for SoliloquyWP and Envira Gallery.

Comments

    Deepak

    August 22, 2016

    No. That plugin didnt worked for me atleast. i tried but sadly it did not show any thumbnails or featured image. i am currently trying some other plugins as i dont want to dig into the codes as of now ๐Ÿ™‚
    thanks

    Reply

    Andrei Filonov

    August 22, 2016

    Unfortunately adding condition “has_post_image” may be tricky when plugins are used for “latest” or “related” posts, as we don’t want to mess up plugin code ๐Ÿ™‚

    So, default image plugin may be better in this case.
    Alternatively, here is some custom code to get the same results:

    How to Display a Default Post Thumbnail in WordPress Without Setting a Featured Image for Every Post

    You can use it as a starting point and expand if you want custom default images for different post types or categories.

    Reply

    Sagar Patil

    August 22, 2016

    i used that plugin Default featured image. its good.

    Reply

Leave a Reply

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

[i]
[i]