如何显示 Woocommerce 类别图像? [英] How to display Woocommerce Category image?

查看:24
本文介绍了如何显示 Woocommerce 类别图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 PHP 中使用此代码:

I use this code in PHP:

$idcat = 147;
$thumbnail_id = get_woocommerce_term_meta( $idcat, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
echo '<img src="'.$image.'" alt="" width="762" height="365" />';

其中147是手动设置的当前ID,但我需要在其他类别中设置当前ID

Where 147 is the current ID manually set, but i need to current id in other categories

有什么建议吗?

推荐答案

要在 archive-product.php 中显示当前显示类别的类别图像,请使用当前类别 term_idis_product_category() 为真时:

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

这篇关于如何显示 Woocommerce 类别图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆