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.

About the Author

Jonathan Daggerhart

Long time Drupal and WordPress developer. I like to write modules and plugins, and I dabble in frontend and design.

12 thoughts on “WordPress: Add Images to Taxonomy Terms

  • Ajith

    works totally fine

  • btheath

    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

      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

    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

    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

      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

  • irfan sodawala

    Hi,Jonathan

    How to assign term image to multiple taxonomies.
    Thanks

    • Jonathan Daggerhart

      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

      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

    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

      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.

Leave a Reply

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