How to display Woocommerce Category image?

后端 未结 9 1493
说谎
说谎 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:33

    To prevent full size category images slowing page down, you can use smaller images with wp_get_attachment_image_src():

    <?php 
    
    $thumbnail_id = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true );
    
    // get the medium-sized image url
    $image = wp_get_attachment_image_src( $thumbnail_id, 'medium' );
    
    // Output in img tag
    echo '<img src="' . $image[0] . '" alt="" />'; 
    
    // Or as a background for a div
    echo '<div class="image" style="background-image: url("' . $image[0] .'")"></div>';
    
    ?>
    

    EDIT: Fixed variable name and missing quote

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

    To display the category image for the currently displayed category in archive-product.php, use the current category term_id when is_product_category() is true:

    // verify that this is a product category page
    if ( is_product_category() ){
        global $wp_query;
    
        // get the query object
        $cat = $wp_query->get_queried_object();
    
        // get the thumbnail id using the queried category term_id
        $thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true ); 
    
        // get the image URL
        $image = wp_get_attachment_url( $thumbnail_id ); 
    
        // print the IMG HTML
        echo "<img src='{$image}' alt='' width='762' height='365' />";
    }
    
    0 讨论(0)
  • 2020-12-08 08:38

    <?php 
    
    	$terms = get_terms( array(
    	'taxonomy' => 'product_cat',
    	'hide_empty' => false,
    	) ); // Get Terms
    
    foreach ($terms as $key => $value) 
    {
    	$metaterms = get_term_meta($value->term_id);
    	$thumbnail_id = get_woocommerce_term_meta($value->term_id, 'thumbnail_id', true );
    	$image = wp_get_attachment_url( $thumbnail_id );
    	echo '<img src="'.$image.'" alt="" />';
    } // Get Images from woocommerce term meta
    
    ?>

    0 讨论(0)
提交回复
热议问题