php - 如何显示 Woocommerce 类别图像?

标签 php wordpress woocommerce thumbnails

我在 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

有什么建议吗?

最佳答案

要在 archive-product.php 中显示当前显示类别的类别图像,请在 is_product_category() 时使用当前类别 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 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' />";
}

关于php - 如何显示 Woocommerce 类别图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12717112/

相关文章:

navigation - Woocommerce 获取下一个/上一个产品 SAME 类别

phpmyadmin 错误 : cant create table error number 150

php - preg_match() 和 preg_replace() 慢吗?

带有 while 循环的 php 回显表

php - 日期格式时区问题

wordpress - 如何添加或删除 woocommerce 错误消息

WordPress 添加 php 文件并链接到它们

jquery - 菜单移动样式,但也处于桌面全高

用于编辑帖子元的 WordPress 插件

jquery - 如何在 WordPress WooCommerce 结帐页面上使用 jQuery