前段時間進行改造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提供的工具進行測試:結構化數據測試工具。
评论