/Add a WordPress Default Featured Image
Wordpress Featured Image

Add a WordPress Default Featured Image

Anyone who ever used / managed / built a WordPress website before, known what a featured image is. A featured image is an image that is associated with a certain post / post type and makes it easy for a user to spot content that interests him. In some cases it could possibly slip our mind and we could publish a post without adding a featured image. Depending on how the theme you are using is built, forgetting to add a featured image could either completely break your site, leave an empty area or in the best case scenario it will have a fallback / default image which will be used instead.

Having a default image is a great idea and even if you have no coding experience, this shouldn’t be too hard to do.

Let’s start

  1. First of all we need to create a default image that will be used in the chance that we don’t add one
  2. Then we need to upload it. This can be done via FTP and put in our theme folder or just upload via the built-in media uploader
  3. Now all we have to do is create a simple if statement that will show our default featured image if the regualr one isn’t available. Find the correct place in your files (probably within single.php ) and paste the following code:
    <?php 
    if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    } else { ?>
    <img class="default-image" src="your-full-image-path.jpg" alt="<?php the_title(); ?>" />
    <?php } ?>

    This is all you really need and will replace your featured image if needed. Just make sure to add your actual default image URL in the code.

We are going to take this up a notch and create a function

Instead of using that code, let’s create a cleaner looking file. First, go to your functions.php file and paste this at the bottom:

function default_image(){
if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img class="default-image" src="your-full-image-path.jpg" alt="<?php the_title(); ?>" />
<?php } 
}

Now, in your single.php file, or wherever you have a featured image, all you need to do is call the function we just created, like so: <?php default_image(); ?>

That’s it.

So from now on when creating themes or even managing them, you can add your own default WordPress featured image in just a few minutes.

 

 

Join Thousands of Others and Never Miss a Post