谷姐:我们都是谷歌迷
We are all Google fans

解决AMP报错Error in required structured data element

博客最近开启了AMP,但是发现在Google Search Console中,关于AMP有很多的报错
Error in required structured data element

摸索

点开Learn More的链接,跳到了一个页面,很多新概念,不想看啊。想到之前有一个专门检测AMP错误的工具,凭借零零碎碎的关键词,终于找到了该工具。立马丢了一个有问题的链接进去,提示Not all markup is eligible for rich results,普通文章显示有一个警告,并且对应的AMP文章有一个错误
Not all markup is eligible for rich results

解决

点看错误,发现给的具体错误提示Missing field ‘image’。看不懂啊
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://imgs.eatash.com/uploads/20210116175638.jpg';

			if ( has_post_thumbnail( $queried_object ) ) {
				$url = get_the_post_thumbnail_url( $queried_object );
			}
				$metadata['image'] = [
					'@type' => 'ImageObject',
					'url'   => $url,
				];
		}
		return $metadata;
	}
);

重新检测,可能要好几天才可完成全部检测
Validation started Started: 11/04/2021

检测完成,邮件通知:AMP issues successfully fixed
检测完成,AMP issues successfully fixed for site

参考

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/

未经谷姐允许不得转载:谷姐靓号网 » 解决AMP报错Error in required structured data element
分享到: 生成海报

热门文章

评论 抢沙发

评论前必须登录!

立即登录   注册

买Google Voice认准【谷姐靓号网】

Google Voice靓号列表Google Voice自助购买
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活