前段时间进行改造MediaWiki网站符合百度熊掌号验证的时候接触到ld+json数据,这是加到head里面的一种结构化数据,用来告诉搜索引擎一些关于页面更新时间等元素。
后来在Google Search Console里面收到这样的报错提示“检测到了新的AMP问题:必需的结构化数据元素有误”,仔细查看是“所链接到的 AMP 版本有效,但收到了警告
It is eligible for some AMP-specific features in Google Search results, but is not fully compliant with AMP best practices. 了解详情”,这里可以看到AMP和非AMP结构化数据的文档和例子。
在MediaWiki中我们是安装了扩展程序Google Rich Cards,Drupal里面可以找到类似的第三方模块,但安装后发现比较复杂,还不如自己直接修改显示模板,把ld+json数据写入head里面,在模板文件中示范例子如下:
...
$pub_date_display = ...
$up_date_display = ...
$description = ...
$main_url = strtok("https://$server_name$request_uri",'?');
$site_name = variable_get('site_name', 'Drupal');
$json = '<script type="application/ld+json">{
"@context": "http://schema.org",
"mainEntityOfPage": "'.$main_url.'",
"@type": "Article",
"headline": "'.$title.'",
"datePublished": "'.$pub_date_display.'",
"dateModified": "'.$up_date_display.'",
"description": "'.$description.'",
"author": {
"@type": "Person",
"name": "James Qi"
},
"image": {
"@type": "ImageObject",
"url": "https://'.$server_name.'/logo.png",
"width": "60",
"height": "60"
},
"publisher": {
"@type": "Organization",
"name": "'.$site_name.'",
"logo": {
"@type": "ImageObject",
"url": "https://'.$server_name.'/logo.png",
"width": "60",
"height": "60"
}
}
}</script>
';
$meta= "
<meta name=\"keywords\" content=\"$meta_keywords\" />
<meta name=\"description\" content=\"$meta_description\" />
";
if ($theme == 'ampsubtheme_example') {
$meta .= $json;
} else {
$meta .= "$xiongzhanghao\n$json";
}
$element = array(
'#type' => 'markup',
'#markup' => $meta,
);
drupal_add_html_head($element, 'meta');
...
这样修改保存后可以使用Google提供的工具进行测试:结构化数据测试工具。
评论