首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WooCommerce:在Yoast中将品牌和gtin添加到产品模式标记

WooCommerce:在Yoast中将品牌和gtin添加到产品模式标记
EN

Stack Overflow用户
提问于 2021-05-27 20:24:18
回答 2查看 499关注 0票数 0

我想在WooCommerce中将品牌和gtin添加到我的产品标记中。目前,Yoast SEO插件已经添加了很多标记。但它只能选择添加具有产品属性的品牌。在我的例子中,品牌是基于自定义字段的。另外,gtin在不同的字段中,不能与Yoast一起使用。

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

add_filter( 'wpseo_schema_webpage','example_change_webpage‘);

代码语言:javascript
运行
复制
/**
 * 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块产品的一个示例:

代码语言:javascript
运行
复制
{
      "@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添加到头部。如下所示:

代码语言: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的现有标记中?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-08-13 11:29:14

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

代码语言:javascript
运行
复制
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;
}
票数 2
EN

Stack Overflow用户

发布于 2021-05-27 21:09:39

我想我找到了一个解决方案:

代码语言:javascript
运行
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67722063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档