wordpress - 从 Yoast SEO 插件生成的文章架构中删除 datePublished 和 dateModified
问题描述
我正在尝试从 Yoast SEO 插件生成的文章模式中删除日期属性。
在他们的开发人员文档中,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' );
这根本没有反映到文章模式中。如何成功删除这些属性?
解决方案
我正在使用此代码,它将起作用
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;
}
推荐阅读
- c# - 我无法在我的 wpf c# 应用程序中使用 firesharp 库检索 firebase 数据
- python - 使用 python API 时 pingAll 失败
- python-3.x - 创建循环加载 whit tkinter
- android - 状态栏和工具栏在滚动时从透明变为不透明,就像在 Letterboxd 中一样
- ionic-framework - 如何更改 IONIC 4 中的范围组件代码?
- html - 我的页脚没有出现在页面底部,而是出现在页面中间
- c++ - OpenGL不渲染任何东西
- python - Python函数参数类型依赖
- kotlin - 如何在 Kotlin 中将字节大小转换为人类可读的格式?
- javascript - Node.js express-generator - 由于 TypeError,应用程序无法运行:app.set 不是函数