How to display Woocommerce Category image?

后端 未结 9 1473
说谎
说谎 2020-12-08 07:41

I use this code in PHP:

$idcat = 147;
$thumbnail_id = get_woocommerce_term_meta( $idcat, \'thumbnail_id\', true );
$image = wp_get_attachment_url( $thumbnail         


        
相关标签:
9条回答
  • 2020-12-08 08:13

    This solution with few code. I think is better.

    <?php echo wp_get_attachment_image( get_term_meta( get_queried_object_id(), 'thumbnail_id', 1 ), 'thumbnail' ); ?>
    
    0 讨论(0)
  • 2020-12-08 08:14

    Add code in /wp-content/plugins/woocommerce/templates/ loop path

        <?php
            if ( is_product_category() ){
    
                global $wp_query;
                $cat = $wp_query->get_queried_object();    
                $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 
                $image = wp_get_attachment_url( $thumbnail_id ); 
                echo "<img src='{$image}' alt='' />";
            }
        ?>
    
    0 讨论(0)
  • 2020-12-08 08:24

    From the WooCommerce page:

    // WooCommerce – display category image on category archive
    
    add_action( 'woocommerce_archive_description', 'woocommerce_category_image', 2 );
    function woocommerce_category_image() {
        if ( is_product_category() ){
          global $wp_query;
          $cat = $wp_query->get_queried_object();
          $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
          $image = wp_get_attachment_url( $thumbnail_id );
          if ( $image ) {
              echo '<img src="' . $image . '" alt="" />';
          }
      }
    }
    
    0 讨论(0)
  • 2020-12-08 08:28

    get_woocommerce_term_meta is depricated since Woo 3.6.0.

    so change

    $thumbnail_id = get_woocommerce_term_meta($value->term_id, 'thumbnail_id', true );
    

    into: ($value->term_id should be woo category id)

    get_term_meta($value->term_id, 'thumbnail_id', true)
    

    see docs for details: https://docs.woocommerce.com/wc-apidocs/function-get_woocommerce_term_meta.html

    0 讨论(0)
  • 2020-12-08 08:29

    Use this code this may help you.i have passed the cat id 17.pass woocommerce cat id and thats it

       <?php
          global $woocommerce;
          global $wp_query;
          $cat_id=17;
          $table_name = $wpdb->prefix . "woocommerce_termmeta";
          $query="SELECT meta_value FROM {$table_name} WHERE `meta_key`='thumbnail_id' and woocommerce_term_id ={$cat_id} LIMIT 0 , 30";
          $result =  $wpdb->get_results($query);
    
          foreach($result as $result1){
              $img_id= $result1->meta_value;
          }     
    
          echo '<img src="'.wp_get_attachment_url( $img_id ).'" alt="category image">';
       ?>
    
    0 讨论(0)
  • 2020-12-08 08:31

    You may also used foreach loop for display category image and etc from parent category given by parent id.

    for example, i am giving 74 id of parent category, then i will display the image from child category and its slug also.

    **<?php
    $catTerms = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'child_of'=>'74'));
    foreach($catTerms as $catTerm) : ?>
    <?php $thumbnail_id = get_woocommerce_term_meta( $catTerm->term_id, 'thumbnail_id', true ); 
    
    // get the image URL
    $image = wp_get_attachment_url( $thumbnail_id );  ?>
    <li><img src="<?php echo $image; ?>" width="152" height="245"/><span><?php echo $catTerm->name; ?></span></li>
    <?php endforeach; ?>** 
    
    0 讨论(0)
提交回复
热议问题