How to Set Fallback Featured Image Based on Post Category in WordPress

Recently, considered one of our customers requested us how they may be able to set default fallback submit thumbnail for explicit classes in WordPress. In our earlier educational, we confirmed how to set a default fallback symbol for WordPress submit thumbnails educational. In this text, we will be able to display you the way to set default fallback featured symbol for explicit classes in WordPress.

Note: This is an intermediate stage educational that can require you to know HTML, CSS, and the fundamentals of WordPress theme construction.


Let’s say you’ve a weblog the place you assign a unmarried class to every of your submit (take a look at our information on Categories vs Tags). You can display a fallback symbol primarily based on which class a submit is assigned to.

It is especially helpful when you find yourself steadily confronted with state of affairs when there’s no featured symbol to be had for a submit. Your branded symbol would possibly not fit the theme of the submit, however if you’re the usage of a class explicit symbol, then it’ll nonetheless glance related.

Setting Category Images in WordPress with no Plugin

Previously on WPBeginner, we confirmed you to set class pictures in WordPress. However, for this educational you can want to arrange class pictures manually with no plugin. Check out our Theme Cheat Sheet educational and amateur’s information to pasting snippets in WordPress.

First factor you wish to have to do is create pictures to your classes. Use class slug as your symbol record identify and save all of them in the similar layout, e.g. jpg or png.

Now the issue is that your WordPress theme is also the usage of other symbol sizes in other templates. Like for instance, you might have smaller submit thumbnails on the archive pages and bigger featured pictures on the one posts. We will let WordPress care for the re-sizing of pictures. To do this you wish to have to add your class pictures to your WordPress web site from Media » Add New. During the add, WordPress will retailer your class pictures, and create sizes outlined through your theme and the ones below Settings » Media display.

After importing class pictures, you wish to have to transfer them to a distinct listing. Connect to your website online the usage of an FTP consumer like Filezilla and cross to /wp-content/uploads/ folder. The class pictures you uploaded shall be saved in the month folder. Example: /uploads/2013/12/

Create a folder on your pc’s desktop and identify it category-images. Now obtain your whole class pictures and all of the sizes WordPress created for them to this new folder on your desktop. Once the obtain is done, you wish to have to add the category-images folder to your /wp-content/uploads listing. Doing this will likely permit you to have your whole class symbol sizes in a separate folder which is simple to name into your theme.

Displaying Catagory Images in WordPress Templates

Before we transfer on to set those pictures as default fallback pictures, we could check out how you can show them in your topics. For instance, you’ll show those pictures on the best of your class pages.

if ( is_category() ) 

$thiscat = get_category(get_query_var('cat'),false); 


<img magnificence="category-thumb" src="<?php echo bloginfo('url'); ?>/wp-content/uploads/category-images/<?php echo $thiscat->slug ; ?>-50x50.jpg" alt="<?php echo $thiscat->identify; ?>" />

This is the way it gave the impression on our demo web site’s class archive web page.

Displaying a category icon image on category page

Displaying Category Image as Default Fallback Featured Image

Now we’re going to display you the way to show a class symbol because the default fallback featured symbol or submit thumbnail when a submit does now not have its personal featured symbol.

Note: Please backup your theme recordsdata ahead of making any adjustments.

Inside your loop, the place your theme is exhibiting the featured symbol or submit thumbnail, change it with this code:

<?php if ( has_post_thumbnail() ) : ?>

<div magnificence="entry-thumbnail">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>

<?php else : 
$class = get_the_category(); 
<div magnificence="entry-thumbnail">
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('url'); ?>/wp-content/uploads/category-images/<?php echo $class[0]->category_nicename ; ?>-150x150.jpg" alt="<?php the_title(); ?>" /></a>

<?php endif; ?>

This code seems for a submit thumbnail. If it reveals one, then it presentations the submit thumbnail. Otherwise, it seems for the class a submit belongs to after which presentations the class symbol. We have added -150×150 in the picture record identify as a result of that is the submit thumbnail measurement in our demo theme. Your theme is also the usage of a distinct measurement for submit thumbnails, so you wish to have to use that measurement as an alternative.

Please word that your theme would possibly have already got <?php if ( has_post_thumbnail() ) : ?> line and the following few traces that show submit thumbnail. You can skip the ones traces in case your theme already were given them.

That’s all, we are hoping this text helped you upload fallback featured symbol primarily based on submit class. For comments and questions, please go away a remark beneath.

Recent Articles

The Best Pinterest Alternatives to Use if You are Bored of Pinterest

Pinterest is a loved social media network among the lover of aesthetics, mostly females tbh. Now, however more and more people are...

Ways you can add links to your Instagram post in 2021

Instagram has many cool features which need no introduction. However, there are a number of features that...

Finance App You should Have on Your iPhone and Android

We live in the age of digitization. When everything is digital, it is no surprise that money has joined the club too....

5 Video Editing Softwares to Try Your Hand at in 2021

Video is the king when it comes to social media. With reels dominating Instagram and Facebook and...

Charge Your Laptop Without a Charger in 5 Ways

Forgetting your laptop charger is a pretty common thing. We all have been there and all of...

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here