首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Yoast插件生成的文章模式中删除datePublished和dateModified

从Yoast插件生成的文章模式中删除datePublished和dateModified
EN

Stack Overflow用户
提问于 2021-03-25 19:15:51
回答 2查看 954关注 0票数 1

我试图从由Yoast插件生成的文章模式中删除日期属性。

在他们的开发人员文档中,wpseo_schema_article过滤器被设置为使用文章图块操作的示例。然而,即使有了这个type="application/ld+json"

代码语言:javascript
复制
<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>

当我试图像这样访问和操作数据时:

代码语言:javascript
复制
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中,也不会取消日期设置;就好像过滤器完全被忽略了一样。

更令人困惑的是,在网页模式中对日期的操作是有效的,并且类似于上面的内容:

代码语言:javascript
复制
add_filter( 'wpseo_schema_webpage', 'remove_webpage_dates');

function remove_webpage_dates( $data ) {

    unset($data['datePublished']);
    unset($data['dateModified']);

    return $data;
}

我试过的其他东西包括:

代码语言:javascript
复制
add_filter( 'wpseo_schema_article_date_published', '__return_false' );

add_filter( 'wpseo_schema_article_date_modified', '__return_false' );

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

EN

回答 2

Stack Overflow用户

发布于 2021-09-24 15:08:35

我正在使用这个代码,它会工作的。

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

Stack Overflow用户

发布于 2022-09-24 02:57:22

试试这个精确的密码。我相信它会对你有用的。这至少解决了我的问题。

代码语言:javascript
复制
// 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;
}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66806184

复制
相关文章

相似问题

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