Question

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?

Était-ce utile?

La solution

The images where setted by woocommerce,

In case someone needs, this is how I did it

$thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
$term_img = wp_get_attachment_url(  $thumb_id );

Autres conseils

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.

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.

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 );

<?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;  ?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top