首页 > 解决方案 > 从 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' );

这根本没有反映到文章模式中。如何成功删除这些属性?

标签: wordpressyoast

解决方案


我正在使用此代码,它将起作用

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;
}

推荐阅读