博客最近开启了AMP,但是发现在Google Search Console中,关于AMP有很多的报错
目录
摸索
点开Learn More的链接,跳到了一个页面,很多新概念,不想看啊。想到之前有一个专门检测AMP错误的工具,凭借零零碎碎的关键词,终于找到了该工具。立马丢了一个有问题的链接进去,提示Not all markup is eligible for rich results,普通文章显示有一个警告,并且对应的AMP文章有一个错误
解决
点看错误,发现给的具体错误提示Missing field 'image'。看不懂啊
尝试性丢到Google里面搜了下,找了几个解决方案,其中一个点赞很多的方法,让尝试在 function.php 里面增加一个函数,使得AMP文章如果找不到post_thumbnail,就用一个默认的图片替代。于是打开对应文件,新增
add_filter( 'amp_schemaorg_metadata', function ( $metadata ) { $queried_object = get_queried_object(); if ( $queried_object instanceof WP_Post ) { // Featured image Place holder. if you don't have featured image. $url = 'https://www.goojie.eu/wp-content/uploads/imgs/20210116175638.jpg'; if ( has_post_thumbnail( $queried_object ) ) { $url = get_the_post_thumbnail_url( $queried_object ); } $metadata['image'] = [ '@type' => 'ImageObject', 'url' => $url, ]; } return $metadata; } );
检测完成,邮件通知:AMP issues successfully fixed
参考
1. https://ampforwp.com/tutorials/article/error-in-required-structured-data-element/
2. https://wordpress.org/support/topic/amp-version-has-invalid-structured-data-5/