You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce单品页显示产品属性的ACF品牌Logo字段

解决WooCommerce产品品牌Logo单品页显示问题(短代码返回null)

核心问题排查

短代码返回null大概率是未正确关联产品属性术语与ACF字段,或获取字段的参数错误。先确认以下前提:

  • ACF字段需绑定到产品属性(品牌)的术语上,而非产品本身。创建字段时,位置规则选「分类法 → 等于 → 你的品牌属性(如pa_brand)」
  • 目标产品已正确选择对应的品牌属性值
  • ACF字段返回格式确实设为「图片URL」

正确短代码写法

替换代码中的pa_brand(你的品牌属性slug)和brand_logo(你的ACF字段名),添加到主题functions.php或自定义插件中:

function display_brand_logo_shortcode() {
    global $product;
    if ( ! $product ) return '';

    // 替换为你的品牌属性slug(WooCommerce属性slug以pa_开头)
    $brand_taxonomy = 'pa_brand';
    // 获取当前产品的品牌术语ID
    $brand_terms = wp_get_post_terms( $product->get_id(), $brand_taxonomy, array( 'fields' => 'ids' ) );

    if ( empty( $brand_terms ) ) return '';

    // 取第一个品牌术语(多品牌场景可自行调整逻辑)
    $brand_term_id = $brand_terms[0];
    // 关键:ACF术语字段需用term_前缀拼接ID
    $logo_url = get_field( 'brand_logo', 'term_' . $brand_term_id );

    if ( empty( $logo_url ) ) return '';

    return '<img src="' . esc_url( $logo_url ) . '" alt="品牌Logo" class="product-brand-logo" />';
}
add_shortcode( 'product_brand_logo', 'display_brand_logo_shortcode' );

调试方法(若仍返回空)

在短代码中加入调试语句,定位问题环节:

// 输出产品的品牌术语ID,确认是否获取到
var_dump($brand_terms);
// 输出ACF字段值,确认字段是否正确关联
var_dump(get_field( 'brand_logo', 'term_' . $brand_term_id ));

使用方式

在产品单品页的模板文件、自定义HTML模块或编辑器中,添加短代码:[product_brand_logo]即可显示品牌Logo。

内容的提问来源于stack exchange,提问作者Zerion

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 03:05:20