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

时间:2019-05-26 13:59:42

标签: woocommerce google-search

是否可以在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代码段创建正确的标记有任何想法吗?

1 个答案:

答案 0 :(得分: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;
}