我想在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的现有标记中?
发布于 2021-08-13 11:29:14
除非你使用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;
}
发布于 2021-05-27 21:09:39
我想我找到了一个解决方案:
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;
}
https://stackoverflow.com/questions/67722063
复制相似问题