Home / Articles / WordPress / How Do You Set A Default Featured Image In WordPress ?

How Do You Set A Default Featured Image In WordPress ?

Most WordPress Themes released these days have the ability to add a featured image to each post. In this tutorial, I'll show you how you can add a default featured image and how to set the first image of a post as the featured image by default.

Featured Images Are Pretty Important

Featured images add to the visual appeal of each post and help the visitor identify the topic of conversation with a cursory glance. How many times have you clicked on a post with an unappealing featured image ?

I'm willing to bet not too often. The best blogs on the web use custom graphics, for example Elegant Themes' Blog has awesome featured images. These images are produced by a team of in-house designers. You'll notice that they are all unique and exquisitely crafted.

But that's not the topic of this post. If you should ever fail to add a featured image to a post, it looks very bad on your blog or site. So if you should ever forget to do so, we can add a default featured image that might mitigate the fallout.

[success icon=”exclaimation-sign”]

Need Help Tweaking Your WordPress Site?
WHSR is now partner with Codeable.io to assist users who need professional WP development / customization services.

To get a free quotation, please fill up this request form.



Setting A New Custom Default Image

You can do this, by installing & activating Default Featured Image Plugin.

Before I get to the plugin, let's have a look at a test site without any default featured images.

This is how the test website looks before a default featured image is added. With this particular theme (Solon), it isn't as bad at it can be. With some WordPress themes, you'll have blank images instead of no images with each post, which has the potential to make things even worse.



Default Featured Image Plugin

1. Go to Settings > Media.
2. Under Media Settings,  you can select a default featured image from the gallery or you can upload one.


And, you are set. It really is that simple.

Now with default featured image in place, this is how the page looks:



You can choose an image that would look good on almost any type of post.

For example, if your blog revolves around the WordPress niche, you can create a custom image with the text, “WordPress Tips & Tutorials”.

Now something like that would look good on your blog year around should you ever forget to add a featured image to your post.

Setting Your Post's First Image as the Default Featured Image

This can be done with two pieces of code.

The first one, you'll have to add to functions.php in your WordPress file.

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

The above code helps get your first image from each post.

Now we need to check if there are any featured images selected for the posts in question.

You can do that by adding the following code to your theme file.  Please add the code to the place where you'd like to display the image aka your home.php, single.php, archive.php or wherever else you deem it necessary.

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

The second piece of code checks the posts for featured images and if there is no featured image, then it uses the default featured image which is created by the first code.

So every time you forget to add a featured image, the first image of the same post is set as the featured image.

Other Ideas For Featured Images

Now there's something else you can try as well, if you are comfortable with handling WP code. You can create conditional featured images. Engage WP has an great tutorial on the this topic.

Tell me how it went with your blog, did you manage to find the right default featured image ?

If you've used a different custom script to add a default featured image, I'd love to hear about it in the comments.

Source (Code For Setting ) : Snipplr

Photo of author

Article by Vishnu

Keep Reading