How To Add an Image to a WordPress Category EasilyWritten by: zacbanas on September 2, 2019
WordPress categories are not like regular posts, there are no “featured image” inputs. With the magic of Advanced Custom Fields, a plugin for adding custom input fields, we can add the image area. The real trick is displaying that image onto your category page. In this short article I will show you how to add an image to a WordPress category.
Advanced Custom Fields
- Install and activate Advanced Custom Fields
- Create a new field group named “Category”
- Add a field named “Category Image”
- In rules, show the field if taxonomy is equal to category
Go to your desired category and set whichever image you want to be your categories image.
Now comes the only coding you will need to do during this whole process.
Open up your archives.php file and enter the following right below get_header().
// Gets the object that you want // It comes back as an object of associative arrays $term = get_queried_object();
Now go to where you want the image to appear and enter the following:
<div style="background-image: url(<?php echo get_field('category_image', $term); ?>); height: 300px"></div>
This code calls the image you added to your category via Advanced Custom Fields and displays it as a background image. Of course you can change the HTML, make it an <img> tag if you want or change the height.
This is the easiest and safest way to add an image to a WordPress category. It took a lot of trial and error, using other methods but this one came out on top. WordPress should have the ability to add and image to a WordPress category built in. It doesn’t make much sense why they wouldn’t have it come in every install.
Like all great things in life, nothing comes easy. If this post helped you, let me know below. Make sure to check out my other posts as well, they’re full of great bits of knowledge!