如何将“ Brand”元素添加到架构标记(WooCommerce微数据)

问题描述 投票:0回答:1

是否有可能在WooCommerce产品模式中添加有关产品品牌的信息?

我曾尝试使用产品架构插件,但它们不符合我的需求,破坏了现有的架构或在产品代码中添加了第二个,因此最好的解决方案是手动修改它。我尝试使用此代码段,但它没有创建正确的标记结构:

add_filter( 'woocommerce_structured_data_product_offer', 'nt_woocommerce_structured_data_product_offer', 10, 2 );

function nt_woocommerce_structured_data_product_offer( $markup, $product ) {


    $markup[ 'brand' ] = wc_get_product()->get_attribute('pa_brand');

    return $markup;

}

我需要此代码段来创建类似这样的内容:

"brand": {
    "@type": "Thing",
    "name": "ACME"
  }

但是它创建了这样的标记,未经Google验证:

"brand":"ACME"

您对如何使用php代码段创建正确的标记有任何想法吗?

woocommerce google-search
1个回答
0
投票

您需要使用其他过滤器woocommerce_structured_data_product并使用数组设置品牌。

add_filter( 'woocommerce_structured_data_product', 'nt_woocommerce_structured_data_product_offer', 10, 2 );
function nt_woocommerce_structured_data_product_offer( $markup, $product ) {

    $markup[ 'brand' ] = array(
        '@type'  => 'Thing',
        'name'   => wc_get_product()->get_attribute('pa_brand'),
    );

    return $markup;
}
© www.soinside.com 2019 - 2024. All rights reserved.