Assign Default Featured Images to Post by Category

I have been working on a new website recently – On My Bike. It’s a personal motivation website, I’m doing a lot of thinking, reflecting, etc. while I shop for a new bicycle. Once I get the bike I want to monitor some stats, etc. So I use IFTTT to relay data from my bike rides, scale, etc. to that new WordPress site. Those posts are automatic and I don’t get to set my featured image as they post. The theme I am using really looks best when featured images are included, so I wanted to add specific featured images per category since my auto posts are assigned their own categories. Here’s how I got that done.

A little disclaimer ahead of time. I’ve had this snippet of code saved for years. I keep a log of code I come across that I might need some day. Back then, I didn’t keep notes on where I got the code from (I do now). I like to give credit, but I don’t have the deets. If this is yours let me know and I’ll link to your article!

Now, onto the code! We need some preliminary action. You need the category ID for each of the categories you will use and you need the attachment ID for each image you will use. We are going to use images uploaded through the standard WordPress add media interface so go ahead and upload the images you want to use.

To get your category ID you can head to Posts-Categories from your admin panel and click on the category you want to use. You will go to a category admin page. Inspect that URL which will look like this:

http://domain.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=11&post_type=post

See that number in there? tag_ID=11 – well 11 is the category ID. Now, onto the image. Head to Media-Library from your admin panel and select one of your images, you will head to the image admin page. Inspect that URL which looks like this:

http://domain.com/wp-admin/post.php?post=61&action=edit

See that number again? post=61 – here 61 is the attachment ID. You need to do this for each category to use, and each image to use.  With all that preliminary work done, we are ready to code! You can drop this php into your functions.php in your child theme or a functionality plugin if you use one (I went the plugin route as I’m always going to want to use the same images with the same categories regardless of theme). Just don’t edit an original theme, that’s just bad joo-joo!

function voodoo_category_featured_image() {

	global $post;

	$featured_image_exists = has_post_thumbnail($post->ID);

	if (!$featured_image_exists)  {

	$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );



		if ($attached_image) {



		foreach ($attached_image as $attachment_id => $attachment) {

			set_post_thumbnail($post->ID, $attachment);

			}

		}

		else if ( in_category('7') ) {

		set_post_thumbnail($post->ID, '41');

		}

		else if ( in_category('8') ) {

		set_post_thumbnail($post->ID, '32');

		}

		else {

		set_post_thumbnail($post->ID, '27');

		}

	}



}

add_action('the_post', 'voodoo_category_featured_image');

Basically, we run a check to see if there is a featured image assigned. If you are manually setting up a post and add your featured image, we don’t want to use the default! Then we just add default images per category. So if the post is category 7, we assign image 41. If it’s in category 8, we assign image 32. If you need to add more categories you can repeat these three lines of code over and over following the pattern here (Just keep adding them in before that final ‘else’ code.

		else {

		set_post_thumbnail($post->ID, '27');

		}

and add in your categories and images as needed. That final bit of code in the loop which uses image 27 is the overall default thumbnail. So if the image doesn’t belong to category 7 or 8 in my example and it doesn’t have a featured image assigned to it, image 27 will be used. I just thought it would be handy to have a fallback in case I forget to add an image or I autopost to categories not included here.

Sure, there are a bunch of plugins out there that do this. Some of them haven’t been updated in a while and some just do way more than I needed. I just wanted a real simple way to add in 2 images to 2 categories!

Leave a Reply