6

This is how I am currently trying:

$taxonomies = array( 
   'product_cat'
);
$args = array(
   'orderby'           => 'name', 
   'order'             => 'ASC',
   'hide_empty'        => false, 
   'exclude'           => array(), 
   'exclude_tree'      => array(), 
   'include'           => array(),
   'number'            => '', 
   'fields'            => 'all', 
   'slug'              => '',
   'parent'            => '',
   'hierarchical'      => true, 
   'child_of'          => 0,
   'childless'         => false,
   'get'               => '', 
   'name__like'        => '',
   'description__like' => '',
   'pad_counts'        => false, 
   'offset'            => '', 
   'search'            => '', 
   'cache_domain'      => 'core'
); 

$terms = get_terms($taxonomies, $args);
$print_terms = 0;
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
   foreach ( $terms as $term ) {
     $term_img = wp_get_attachment_url( get_post_thumbnail_id($term->term_id) );
     var_dump($term_img); /* Allways Bool(false) */

So question is,

Do you know what am I doing wrong?

5 Answers 5

15

The images where setted by woocommerce,

In case someone needs, this is how I did it (in 2015)

$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url(  $thumb_id );
3
  • 3
    +1000% :) thanks May 22, 2019 at 6:05
  • really worked man :) Jan 22, 2021 at 12:47
  • 1
    get_woocommerce_term_meta() is now deprecated. You should use get_term_meta() instead :) Jul 4, 2022 at 10:29
2

Taxonomies don't by default have thumbnail images. Without knowing how those are set I can't say exactly how to get the thumbnails, but as for "what am I doing wrong?" get_post_thumbnail_id accepts a post ID or lacking that assumes the current post in the Loop. You are passing it a term_id, which isn't going to work. You can see that in the source:

32  function get_post_thumbnail_id( $post_id = null ) {
33          $post_id = ( null === $post_id ) ? get_the_ID() : $post_id;
34          return get_post_meta( $post_id, '_thumbnail_id', true );
35  }

I suppose, if the term_id happens to match a post ID you'd get something but it isn't going to be what you want or expect.

1
1

The accepted answer is no longer valid since woocommerce_get_term_meta is deprecated. Also there is no longer a meta field of thumbnail_id. Here is the correct solution for 2021 if you are using WooCommerce:

$term_image_id = get_term_meta( $term->term_id, 'product_search_image_id', true ); $term_image = wp_get_attachment_url( $term_image_id );

2
  • So today it would be a different question, right? May 11, 2022 at 8:49
  • No, your question was how to get a taxonomy thumbnail URL, this is how you do so with the latest version.
    – RiotAct
    Aug 1, 2022 at 19:10
0

Woocommerce is not needed to retrieve a category image and its URL.

$categories = get_categories();

foreach($categories as $cat) {

    $image_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
    $post_thumbnail_img = wp_get_attachment_image_src( $image_id, 'thumbnail' );

    echo '<img src="' . $post_thumbnail_img[0] . '" alt="' . $cat->name . '" />';

}

$post_thumbnail_img is an array with key 0 equal to the URL, 1 = width, and 2 = height.

You could also use any of the following in place of 'thumbnail' (featured, medium, large, or any other custom image size from your theme). However, 'thumbnail_id' must remain as-is.

You could also retrieve image metadata such as "alt", "title" and more using a custom function and extend this further.

The above assumes you know how to supply arguements to get_categories in a loop. But, if not take a look at get_categories on WP for more details.

-2
<?php
            $wcatTerms = get_terms('product_cat', array('hide_empty' => 0, 'parent' =>0));

            foreach($wcatTerms as $wcatTerm) : ?>
            <?php
            $thumb_id = get_woocommerce_term_meta( $wcatTerm->term_id, 'thumbnail_id', true );
            $term_img = wp_get_attachment_url(  $thumb_id );
            ?>

                        <div class="product-item">
                          <div class="item-inner fadeInUp">
                            <div class="product-thumbnail">
                              <div class="icon-sale-label sale-left">Sale</div>
                              <!--<div class="icon-new-label new-right">New</div>-->
                              <div class="pr-img-area"> <img class="first-img" src="<?php echo $term_img;?>" alt=""> <img class="hover-img" src="<?php echo $term_img;?>" alt=""> </div>
                            </div>
                            <div class="item-info">
                              <div class="info-inner">
                                <div class="item-title"> <a title="Ipsums Dolors Untra" href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a> </div>
                              </div>
                            </div>
                          </div>
                        </div>

                 <?php endforeach;  ?>
1
  • this is pretty much a duplicate of my answer.. but thanks Feb 20, 2018 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.