Today I wanted to go through another quick WordPress tutorial to do with the post thumbnail feature in WordPress. You may want to set up a blog and use one of the great features in WordPress – the post thumbnail. For any of you who don’t know what this is I shall explain…
When you create a post or page you have the option to attach a featured image to a post and echo out in your web design. The image below is an example of how we at Creare use this on our blog.
![]()
So anyway, back to how we are going to get this functionality to work. It might be that on your website you want to include post thumbnails. You may get an instance where you haven’t got enough time to make an image, or maybe it doesn’t link up properly. Having a fallback in place is for both best practice and preventing any problems occurring on the website.
There’s a blog here which shows you how to reference the thumbnail in your website (there’s also a useful intro which shows you how to include post thumbnails in your template if you are not using an up-to-date version of WordPress).
The code
So first of all I will show you how to include a thumbnail in your post. You can insert this code into your loop to bring out the relevant thumbnail per blog post:
<?php the_post_thumbnail(); ?>
If you have a range of sizes defined in your template, you can just add the reference name in the parameters of the function. i.e:
<?php the_post_thumbnail('blog-thumb'); ?>
So to add a fallback is quite easy. Firstly we want to do a check to make sure our post has an associated image. With that response we can use a simple if statement to say – if we we have a thumbnail, use it. If not, use this replacement.
Here’s the code:
<?php if ( has_post_thumbnail() ): the_post_thumbnail(); else: ?> <img src="/images/default-image.jpg" alt="<?php the_title(); ?>" /> <?php endif; ?>
Using a function already built into WordPress we are checking to see if the thumbnail is linked to the blog post, if not use our default image. You can just create something generic to use when a thumbnail has not been linked up.
I hope you can put this to a little bit of use. Any questions or comments please leave below!


Sir, thank you very much. Very helpful!