简体   繁体   English

自定义WooCommerce主题中的产品类别图片

[英]product category image in custom WooCommerce theme

I am looking for some help finding a php snippet that will display the product category image in a custom WooCommerce theme. 我正在寻找一些帮助,以查找将以自定义WooCommerce主题显示产品类别图像的php代码段。 I am using a plugin that executes the php code within a widget and it is working fine for the product category name. 我正在使用在小部件内执行php代码的插件,并且该产品类别名称的效果很好。 I just can't find anything that works for the category image. 我只是找不到适用于类别图片的任何内容。 Any help would be appreciated. 任何帮助,将不胜感激。

Assuming you know the category ID and it's in $cat_ID : 假设您知道类别ID,并且它在$cat_ID

// get the thumbnail ID
$thumbnail_id = get_woocommerce_term_meta( $cat_ID, 'thumbnail_id', true ); 
// get the image URL
$image = wp_get_attachment_url( $thumbnail_id ); 
// print the IMG HTML
echo '<img src="'.$image.'" />';

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: 要在archive-product.php中显示当前显示类别的类别图像,请在is_product_category()为true时使用当前类别term_id:

// 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 user the 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" />';
    }

Refer below code :- 请参考以下代码:-

global $product;

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="' . esc_url($image) . '" alt="" />';

    }    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM