[英]Remove datePublished and dateModified from Yoast SEO plugin generated Article schema
我正在嘗試從 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;
}
試試這個確切的代碼。 我相信它會為你工作。 這至少解決了我的問題。
// 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; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.