WordPress: Add Images to Taxonomy Terms

I’ve recently been working on an example plugin/library for adding an image to taxonomy terms (such as categories, post tags, or custom taxonomy terms). The plugin adds an image upload field to the create and edit term forms.

taxonomy-term-image-screenshot

As a developer, you can choose which taxonomy has images and change the field text by using the ‘taxonomy-term-image-taxonomy’ and ‘taxonomy-term-image-labels’ filters respectively.

The plugin’s README file provides a few example usages.

GitHub Repo

9 Thoughts

Discussion

Ajith
July 1, 2015

works totally fine

btheath
July 10, 2015

How would I get the image to display on a single product page?

Example, I have several products, each with a specific manufacturer.

Manufacturer is an attribute “pa_manufactuer”, with the names of each manufacturer being the term that has the image.

What I’d like to do is hook into ‘woocommerce_after_add_to_cart_form’ and display the manufacturer image on each single product.

Could you show me what the snippet should look like?

add_action( ‘woocommerce_single_product_summary’, ‘my_function_name’);

function my_function_name() {

What would go here?

}

Jonathan Daggerhart
July 10, 2015

I’m not familiar with that hook (or woocommerce to be honest), but I’m assuming an ad-to-cart form happens on the product page itself. So I think you’d want something like this:

add_action( ‘woocommerce_single_product_summary’, ‘my_function_name’);

function my_function_name() {
  $product = get_post();
  $categories = wp_get_object_terms( $product->ID, 'category' );
  foreach ($categories as  $term) {
    if ( $term->term_image ){
        echo wp_get_attachment_image( $term->term_image, 'thumbnail' );
    }
  }
}

Not positive if that works, but it should get you a lot closer. The main take away here, is that if you get the category objects, it should have a ‘term_image’ property that is the ID for the image you attached to the category.

mrh
November 14, 2015

Found an error when adding new category with the image.

“Notice: Trying to get property of non-object in taxonomy-term-image.php on line 287”

Any idea? Cheers.

Mohammad
January 20, 2016

How to use for specific custom post type and custom taxonomy?

not working:

function music_term_image_taxonomy( $taxonomy ) {
return ‘genre’;
}
add_filter( ‘taxonomy-term-image-taxonomy’, ‘music_term_image_taxonomy’ );

Jonathan Daggerhart
January 21, 2016

Hi Mohammad,

If ‘genre’ is the slug for your taxonomy, that should be working.

One thing I ran into recently is that someone was registering their hooks after they were including the file taxonomy-term-image.php file. This will cause your hook to be “too-late”, and not get called.

I’ve just modified the project to delay the instantiation of the class. Try updating on the master branch and let me know if that works.

Thanks,
Jonathan

Mohammad
January 21, 2016

Hi,
Many thanks, it worked.

irfan sodawala
February 21, 2016

Hi,Jonathan

How to assign term image to multiple taxonomies.
Thanks

Jonathan Daggerhart
March 1, 2016

Hi Irfan,

Sorry for the slow reply. The 2.x branch of the repo allows for multiple taxonomies. I’ll be merging that into the master branch soon.

Jonathan Daggerhart
April 25, 2016

Hi irfan,

I’ve merged the 2.x branch into master. The readme should describe how to enable for multiple post types. Let me know if you run into any issues.

ERT Yazilim
July 14, 2017

Hello.
I prepared a WordPress theme for sale. I used your Taksonomy Term Image project as embedded in this theme. I gave a credit on the introduction page and gave a link to GitHub Project Page. Would you allow me to use your project in this way? What are the terms of use for a WordPress theme designed for sale?

Thank you.
Best regards.

Jonathan Daggerhart
July 19, 2017

Hi Yazilim,

Sorry for the slow reply. Yes, please feel free to use that project in your theme designed for sale. The credit to my github page is perfect, thank you.

Eric
April 1, 2018

Can you help with theme integration? I tried and tried to integrate your plugin directly into the theme but have not had any luck. The add image buttons show up but do not work.

Jonathan Daggerhart
April 4, 2018

The easiest way to use this project would be as a plugin. If you need to include it in the theme because you’re planning to distribute the theme separate from a site, then the best approach would be to place the project within your theme and then include the main plugin file in your functions.php.

For example: If you place this code in your theme within a directory named “taxonomy-term-image”, then in your functions.php file you would need to add the following.

if (file_exists( __DIR__ . '/taxonomy-term-image/taxonomy-term-image.php' ) ) { 
  include_once __DIR__ . '/taxonomy-term-image/taxonomy-term-image.php';
}
Johnny
April 2, 2018

The button does not open the media library

Jonathan Daggerhart
April 4, 2018

Would you please file an issue on the project? https://github.com/daggerhart/taxonomy-term-image/issues

If there are any javascript errors that appear in the browser console when you try clicking the button, please be sure to include that in the issue summary. Any additional information about how you have this setup would be helpful in the issue.

Thanks!

evgeniy
August 23, 2018

Good day, help to inject your image output code into my code

“slug”, “parent” => 0, “hide_empty” => 0)); ?>
$hiterm): ?>

cat_ID ); ?>

<a href="”>
name; ?>

Leave a Reply

Your email address will not be published. Required fields are marked *