I hate not being able to find a good image for the featured image. Most of the posts I write are tutorials with code snippets, not images. As a result, many of my posts can easily not have featured images. Unfortunately, though, that would (and did) cause an inconsistent flow on my post index pages (main blog page, tags, categories, etc.); some posts had images and others didn’t.
As I’ve continued to add more posts over time, this has started to bother me more and more. I decided to try to come up with a way to set a default image for my posts when I don’t set one myself. Additionally, I wanted to set different featured images depending on a post’s category. After searching, I found a few different tutorials on setting default images for WordPress posts. I tried some of those suggestions but had mixed results and none of them worked 100%.
Since I use Genesis, I could have just added the images using a Genesis hook but this doesn’t actually set the image to the featured image. Unfortunately, this would not work for me because I wanted to keep the functionality of my Twitter card (Summary Card with Large Image), which shows the featured image of a post when the link is shared. Therefore, my only option was to make sure that a featured image was set.
Default Featured Images
After not much luck and wasting time with existing snippets, I decided to look at the Default Featured Image plugin from the WordPress repo. It seemed to work for setting a general featured image so I took a look at the code and found a filter that intrigued me enough to download and test.
Once you install and activate the Default Featured Image plugin, it adds a setting under Settings → Media. This setting will let you select a default featured image for all of your posts (including ones already published) when no featured image is set manually. This is the extent of the plugin’s admin settings so there’s nothing else to mess with in the dashboard. The plugin goes to work once you choose your image.
Conditional Featured Images
Once I got my default featured image set, I wanted to test the filter I referred to above. This filter, dfi_thumbnail_id
, appeared to make possible dynamic featured images. I wrote a bit of code and tested it out. Success! This filter did exactly what I had hoped.
I definitely found a new plugin that I really like in Default Featured Image. If you want an easy way to set a general default featured image for your posts, it gives you exactly that. If you also want flexibility in controlling which image is used as a post’s featured image (conditional featured images), here’s a simplified version of the code I wrote for it.
In this sample code, you’re assigning a post’s featured image to a media upload with the ID of 3020 if the post has the category of “tutorials”, 3019 if “reviews” and 3018 if “lists”. You can get as granular with this as you need to.
I hope this post has helped you! If you have questions, leave them below.
Jay Erikson says
Hello,
I wondering if I can display the post author avatar as the default featured image for posts, rather than a specific image.
Thank you.
bojan says
It does not actually set the featured image to the post. When I open post from dashboard, there is no any image set in the featured image post data, but it shows the image in frontend. Why is that?
bojan says
For example, whena I share my post to social there is no image to prewiev. Sorry about my english 🙂
Giang Lê Hoàng says
Hi,
I tried to use the code like below, everything works fine till case = Tiếng Nhật and downward..
Do you have any idea how to fix?
Thank you!
add_filter( ‘dfi_thumbnail_id’, ‘rv_conditional_default_thumbnails’ );
function rv_conditional_default_thumbnails( $object_id ) {
$mon_hoc = get_field( “mam_non_ph” );
$mon_hoc.= get_field( “tieu_hoc_ph” );
$mon_hoc.= get_field( “cap_2_3_ph” );
switch ($mon_hoc) {
case ‘Toán’:
$object_id = 262;
break;
case ‘Vật lý’:
$object_id = 263;
break;
case ‘Hóa học’:
$object_id = 261;
break;
case ‘Sinh học’:
$object_id = 255;
break;
case ‘Văn’:
$object_id = 256;
break;
case ‘Lịch sử’:
$object_id = 257;
break;
case ‘Địa lý’:
$object_id = 258;
break;
case ‘Tiếng Việt’:
$object_id = 259;
break;
case ‘GDCD’:
$object_id = 260;
break;
case ‘Tiếng Anh’:
$object_id = 254;
break;
case ‘Tiếng Anh’:
$object_id = 254;
break;
case ‘Tiếng Trung’:
$object_id = 421;
break;
case ‘Tiếng Nhật’:
$object_id = 422;
break;
case ‘Tiếng Hàn’:
$object_id = 423;
break;
case ‘Âm nhạc’:
$object_id = 424;
break;
case ‘Mỹ thuật’:
$object_id = 425;
break;
case ‘Võ thuật’:
$object_id = 426;
break;
case ‘Luyện Viết’:
$object_id = 420;
break;
default:
}
return $object_id;
}
Amer says
Thank you so much for sharing your experience. I am creating a new website. I needed to create a carousel slider, I purchased plugin called super carousel slider. I chose this slider because it was easy to use, and I am running late in this project and don’t have the time to go through the heavy and complicated process of revolution slider, I am not sure why but there is something I hate about revolution slider.
Anyway, super carousel can generate a slider from the featured images of posts in any category of your choice, so, this is perfect for my use, as I wanted to have all the products in the slider. But those were small images, 250×250. Good for the slider, but not good for the theme, as the theme “using divi” shows really large images for the posts “the featured images”. so, lucky for me divi has an option to disable featured images in the category page. but that left me with a really dull category page, and I need to have a blog/news category for the latest news and I want to show featured images for that specific category.
I have not yet started applying your technique here yet, but I think it shall work.
One question, isn’t better to create a child theme now? since I will be changing php code and divi theme is frequently updated. Is there a way to do this in functions.php in the child theme?
Thanks and keep it up.
Ren says
Yes, you’ll want to use a child theme to avoid any customizations being erased when the parent is updated. You can include custom PHP in functions.php but I suggest a plugin if the functionality is theme-independent, meaning you want to keep it if you switch themes.