我试图从由Yoast插件生成的文章模式中删除日期属性。
在他们的开发人员文档中,wpseo_schema_article过滤器被设置为使用文章图块操作的示例。然而,即使有了这个type="application/ld+json"
<script type="application/ld+json">
{
"@context":"https://schema.org",
"@type":"Article",
"mainEntityOfPage":{
"@type":"WebPage",
"@id":"https://www.myproscooter.com/etwow-electric-scooters-review/"
},
"headline":"E-Twow Electric Scooters 2021 Review",
"image":{
"@type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2020/12/elek-scoot.jpg",
"width":700,
"height":400
},
"datePublished":"2020-12-08T08:52:13",
"dateModified":"2021-01-12T11:30:10",
"author":{
"@type":"Person",
"name":"Jason"
},
"publisher":{
"@type":"Organization",
"name":"MyProScooter",
"logo":{
"@type":"ImageObject",
"url":"https://www.myproscooter.com/wp-content/uploads/2021/01/MPS-Logo-228x60.png"
}
}
}
</script>
当我试图像这样访问和操作数据时:
add_filter( 'wpseo_schema_article', 'remove_article_dates' );
function remove_article_dates( $data ) {
file_put_contents(WP_CONTENT_DIR.'/helper-seo.txt','DATA PRE FILTER: '.print_r($data,true),FILE_APPEND);
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}在文章模式中,没有任何东西会被登录到helper-seo.txt中,也不会取消日期设置;就好像过滤器完全被忽略了一样。
更令人困惑的是,在网页模式中对日期的操作是有效的,并且类似于上面的内容:
add_filter( 'wpseo_schema_webpage', 'remove_webpage_dates');
function remove_webpage_dates( $data ) {
unset($data['datePublished']);
unset($data['dateModified']);
return $data;
}我试过的其他东西包括:
add_filter( 'wpseo_schema_article_date_published', '__return_false' );
add_filter( 'wpseo_schema_article_date_modified', '__return_false' );它根本没有反映到文章模式中。如何成功地删除这些属性?
发布于 2021-09-24 15:08:35
我正在使用这个代码,它会工作的。
add_filter ( 'wpseo_schema_webpage' , 'remove_breadcrumbs_property_from_webpage' , 11 , 1 ) ;
function remove_breadcrumbs_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
unset($data['dateModified']);
}
return $data;
}发布于 2022-09-24 02:57:22
试试这个精确的密码。我相信它会对你有用的。这至少解决了我的问题。
// Remove DatePublished
add_filter( 'wpseo_schema_graph_pieces', 'remove_datePublished_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_datePublished_property_from_webpage', 11, 1 );
/**
* Removes the DatePublished graph pieces from the schema collector.
*
* @param array $pieces The current graph pieces.
* @param string $context The current context.
*
* @return array The remaining graph pieces.
*/
function remove_datePublished_from_schema( $pieces, $context ) {
return \array_filter( $pieces, function( $piece ) {
return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\datePublished;
} );
}
/**
* Removes the DatePublished property from the WebPage piece.
*
* @param array $data The WebPage's properties.
*
* @return array The modified WebPage properties.
*/
function remove_datePublished_property_from_webpage( $data ) {
if (array_key_exists('datePublished', $data)) {
unset($data['datePublished']);
}
return $data;
}
https://stackoverflow.com/questions/66806184
复制相似问题