Discover in this tutorial how to display a default image in the WordPress featured image block with a little bit of code.
The featured image block is very interesting to use it in the query block or as post header.
But, what happens when the content creator forgets to assign a featured image to the post? Well, in the query results, that post will not display the featured image as the others do, thus breaking the desired design for the query.
Luckily, with a little bit of code you can set that, when the post lacks a featured image, it will display one (always the same one) by default. This will not only help you maintain the layout of the query, but it will also help you identify at a glance which post is missing its featured image so you can quickly assign it to it.
Let’s see how to do it.
Snippet to set a default image for the WordPress featured image block
Add the following code at the end of functions.php or in your functionality plugin:
To see this and another 1097 code snippets of this website, login or subscribe here.
In this code you use the post_thumbnail_html
filter to detect if there is a featured image assigned to that post. If it doesn’t find the featured image, the code assigns a default image from a specific location within the theme (/assets/images/default-post-image.webp in the example).
Remember to change this address to the one you want to be your “Default Featured Image”.
You can also assign an image from the media library, just modify its ID in the second snippet.
Conclusion
Now you know how to set a default image for the WordPress featured image block with a little bit of code.
This solution will come in handy when you display a list of posts that include the featured image block if one of your posts doesn’t have such an image assigned to it.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉