We’ve found that setting fallback featured images for WordPress categories is a huge time-saver, especially for blogs with a high volume of content.
If you have articles that don’t have a featured image, or if you want all the posts in a category to have similar thumbnails, then you can set a fallback image for each category.
This way, you can make sure that no blog posts will be left without a featured image and you don’t have to manually assign them to every single post.
In this article, we’ll show you how to set the default fallback featured images for specific categories in WordPress.
Why Add a Fallback Featured Image for Post Category?
Let’s say you have a WordPress blog where you assign a single category to each post. You can assign all posts in that category to show the same fallback image if they don’t have a featured image for some reason.
Another benefit of adding a fallback image for a post category is if your category archive pages get a lot of search traffic, then it makes them more engaging and attractive.
That said, let’s see how you can add a fallback image based on the post category using a WordPress plugin and custom code.
Setting Fallback Featured Image in WordPress Using a Plugin
By default, WordPress doesn’t offer an option to add images to your post category pages. However, you can easily set a fallback image for post categories using a WordPress plugin.
First, you’ll need to install and activate the Category and Taxonomy Image plugin. For more details, please see our guide on how to install a WordPress plugin.
Upon activation, you can head to Settings » Taxonomy Image from the WordPress admin panel. Next, you can click the ‘category’ checkbox to include taxonomy images in your post categories as a fallback.
Don’t forget to click the ‘Save Changes’ button when you’re done.
After that, you can go to Posts » Categories from your WordPress dashboard. You’ll see an ‘Image’ field appear when adding a new category or editing an existing one.
Simply enter an image URL you want to add to your WordPress category. For more details on that, please see our guide on how to get the URL of images you upload in WordPress.
Now, when you publish a blog post with no featured image assigned, then WordPress will use the picture you just set for your category.
Here is a preview of the image we used on our demo site.
That’s all! You’ve now successfully added a fallback image based on the post category.
Setting Fallback Featured Image in WordPress without a Plugin
You can also configure a fallback image for post categories without using a WordPress plugin. However, this method is not recommended for beginners as it involves code snippets.
The first thing you need to do is create images for your WordPress categories. Use category slug as your image file name and save them all in the same format, like JPG or PNG.
Next, you can upload your category images to your WordPress site from Media » Add New.
WordPress will store your category images during the upload and create image sizes defined by your theme.
After uploading category images, you need to move them to a different directory. Simply connect to your website using an FTP client and go to the /wp-content/uploads/ folder.
The category images you uploaded will be stored in the month folder, like /uploads/2022/08/.
Go ahead and open this month’s folder.
You can see our guide on where WordPress store image on your site for more information.
Next, create a folder on your computer’s desktop and name it category-images. Now, download all your category images and all the sizes WordPress made for them to this new folder on your desktop.
Once the download is finished, you need to upload the category-images folder to your /wp-content/uploads directory. Doing this will allow you to have all your category image sizes in a separate folder which is easy to call into your theme.
For more details, please see our guide on how to use FTP to upload files to WordPress.
Displaying Category Image as Default Fallback Featured Image
Next, we will show you how to display one of those images as the fallback featured image when a post in a category does not have one set.
This method does involve copying and pasting code, and we don’t normally recommend users edit their theme files. Small mistakes can make big errors on your site.
An easier way to add code to your site is by using WPCode. It’s the best WordPress code snippet plugin, allowing you to safely and easily manage custom code on your site.
First, you’ll need to install and activate the free WPCode plugin. For more details, please see our guide on how to install a WordPress plugin.
Next, you can go to Code Snippets » + Add Snippet in your WordPress admin panel to add a new snippet. Then just click on the ‘Add New’ button.
After that, you can either add custom code or use a snippet from WPCode library.
For this, you will be using your own custom code, so hover over ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use Snippet’ option.
Next, you need to select ‘PHP Snippet’ as the code type from the list of options that appear on the screen.
Now add a name for your snippet, which can be anything to help you remember what the code is for.
After that, simply copy the following code snippet and paste it into the ‘Code Preview’ field.
/**
* Plugin Name: Category Thumbnail Fallback
* Description: Use the category image as fallback when the post does not have a featured image
*/
class WPBCategoryThumbnailFallback
{
protected static $taxonomies = [‘category’];
protected $nonceId = ‘wpb_category_thumb_fallback_none’;
protected $fieldId = ‘wpb_category_fallback_post_image’;
public $taxonomy;
protected function __construct($taxonomy)
{
$this->taxonomy = $taxonomy;
}
public static function init()
{
foreach (static::$taxonomies as $taxonomy) {
$_self = new self($taxonomy);
add_action(‘admin_enqueue_scripts’, [$_self, ‘scripts’]);
add_action(“{$taxonomy}_add_form_fields”, [$_self, ‘add’]);
add_action(“{$taxonomy}_edit_form_fields”, [$_self, ‘edit’], 99, 2);
add_action(“created_{$taxonomy}”, [$_self, ‘saveTerm’], 10, 2);
add_action(“edited_{$taxonomy}”, [$_self, ‘editTerm’], 10, 2);
add_filter(“get_post_metadata”, [$_self, ‘fallback’], 99, 5);
}
}
public function scripts($hook_suffix)
{
if (in_array($hook_suffix, [‘term.php’, ‘edit-tags.php’])) {
$screen = get_current_screen();
if (is_object($screen) && “edit-{$this->taxonomy}” == $screen->id) {
wp_enqueue_media();
wp_add_inline_script(‘media-editor’, $this->inlineScript());
}
}
}
public function add()
{
?>
Image