WooCommerce:将品牌和 GTIN 添加到 Yoast 中的产品架构标记中

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

我想在 WooCommerce 中向我的产品标记添加品牌和 GTIN。 目前 Yoast SEO 插件已经添加了很多标记。 但它只能选择添加具有产品属性的品牌。就我而言,该品牌基于自定义字段。而且 GTIN 属于不同的领域,不能与 Yoast 一起使用。

我在他们的docs中找到了一个代码片段,它允许将自定义数据添加到标记中:

add_filter( 'wpseo_schema_webpage', 'example_change_webpage' );

/**
 * Changes @type of Webpage Schema data.
 *
 * @param array $data Schema.org Webpage data array.
 *
 * @return array Schema.org Webpage data array.
 */
function example_change_webpage( $data ) {
    if ( ! is_page( 'about' ) ) {
        return $data;
    }

    $data['@type'] = 'AboutPage';

    return $data;
}

但这不适用于产品,我不知道如何改变它。

我还找到了 schmea 件产品的示例:

{
      "@context": "https://schema.org",
      "@graph": [
          {
              "@type": "Product",
              "@id": "https://www.example.com/#/schema/product/abc123",
              "name": "Example Product",
              "image": {
                  "@id": "https://www.example.com/#/schema/image/abc123"
              }
          }
      ]
  }

我可以在自定义函数中使用它,并将其作为 javascript 添加到头部。像这样:

add_action( 'wp_head', function() {

    if ( is_product() ): 
    
    ?>
    
    <script type="application/ld+json">{
          "@context": "https://schema.org",
          "@graph": [
              {
                  "@type": "Product",
                  "@id": "#product",
                  "brand": {
                      "@id": "https://www.example.com/#/schema/organization/abc123"
                  },
                  "sku": "abc123",
              }
          ]
      }</script>
    
<?php
endif;
 }, 99 );

但现在我有两个不同的产品架构标记。

有没有办法将内容添加到 Yoast 的现有标记中?

php wordpress woocommerce schema yoast
3个回答
4
投票

除非您使用 Yoast SEO woocommerce 插件,否则上述批准的答案将不起作用。您可以使用下面的代码添加品牌和 GTIN。

add_filter( 'woocommerce_structured_data_product', 'custom_set_extra_schema', 20, 2 );
function custom_set_extra_schema( $schema, $product ) {

    $gtin       = get_post_meta( $product->get_id(), '_custom_gtin', true );
    $brand_name = get_post_meta( $product->get_id(), '_custom_brand_name', true );

    $schema['brand']  = $brand_name;
    $schema['gtin13'] = $gtin;

    return $schema;
}

1
投票

我想我找到了解决方案:

add_filter( 'wpseo_schema_product', 'custom_set_extra_schema' );
function custom_set_extra_schema( $data ) {
    global $product;
    
    $gtin        = get_post_meta( $product->get_id(), '_custom_gtin', true );
    $brand_name  = get_post_meta( $product->get_id(), '_custom_brand_name', true );

    $data['brand'] = $brand_name;
    $data['gtin13'] = $gtin;
        
    return $data;
}

0
投票
add_filter( 'wpseo_schema_webpage', function( $data ) {
    $data['@type'] = 'Product';
    return $data;
} );
add_filter( 'wpseo_schema_product', 'custom_set_extra_schema' );
function custom_set_extra_schema( $data ) {
    global $product;
    
    $gtin        = get_post_meta( $product->get_id(), '_custom_gtin', true );
    $brand_name  = get_post_meta( $product->get_id(), '_custom_brand_name', true );

    $data['brand'] = $brand_name;
    $data['gtin13'] = $gtin;
        
    return $data;
}
© www.soinside.com 2019 - 2024. All rights reserved.