skip to Main Content

I am using this plugin
https://woocommerce.com/products/custom-product-tag-image/
To add images to my tags.
I’d like to display a list of tags with images. I’ve tried so many approaches but nothing yet.

Here’s my code which outputs a list of tags but no images.

 <?php $taxonomyName = "product_tag";
$terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
<?php foreach ( $terms as $term ) { ?>
<div class="item" > 
 <?php 
            $thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
            $term_img = wp_get_attachment_url(  $thumb_id ); 
        ?>
        <img src="<?php echo $term_img; ?>" />
<a href="<?php echo get_term_link( $term->term_id, 'product_tag' ); ?> " rel="tag"><?php echo $term->name; ?></a>
    </div>
<?php } ?>

Suggestions?

2

Answers


  1. Chosen as BEST ANSWER

    The code below is what worked for me as woocommerce_get_term_meta is deprecated and thumbnail_id doesn't exist anymore:

     <?php $taxonomyName = "product_tag";
    $terms = get_terms(array('taxonomy' => 'product_tag', 'hide_empty' => false)); ?>
    <?php foreach ( $terms as $term ) { ?>
    <div class="item" > 
     <?php 
                $term_image_id = get_term_meta( $term->term_id, 'product_search_image_id', true ); 
                $term_image = wp_get_attachment_url( $term_image_id ); 
            ?>
            <img src="<?php echo $term_image; ?>" />
    <a href="<?php echo get_term_link( $term->term_id, 'product_tag' ); ?> " rel="tag"><?php echo $term->name; ?></a>
        </div>
    <?php } ?>
    

  2. You can use WC get_woocommerce_term_meta() and WP wp_get_attachment_url(). check below code.

    <?php foreach ( $terms as $term ) { ?>
        <div class="item">
            <?php 
                $thumb_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
                $term_img = wp_get_attachment_url(  $thumb_id ); 
            ?>
            <a href="<?php echo $term_img; ?> " rel="tag"><?php echo $term->name; ?></a>
        </div>
    <?php } ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search