在遇到困扰我的问题时,我需要帮助。
我使用以下代码通过Google跟踪代码管理器将所有架构数据动态地插入到我的博客中:
<script>
(function(){
var data = {
"@context": "http://schema.org",
"@type": "BlogPosting",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": {{Page URL}}
},
"headline": {{SCHEMA - Article Headline}},
"image": {
"@type": "ImageObject",
"url": {{SCHEMA - Article Image}},
"height": 300,
"width": 450
},
"datePublished": {{SCHEMA - Article datePublished}},
"author": {
"@type": "Person",
"name": "Gmacp"
},
"publisher": {
"@type": "Organization",
"name": "Optatravel.com",
"logo" : {
"@type": "ImageObject",
"url": "https://www.optatravel.com/wp-content/uploads/2018/08/OPTA-LOGO-SCHEMA-300x60.jpg",
"height": 60,
"width": 300
}
},
"description": {{SCHEMA - Article Description}}
}
var dateModified = {{SCHEMA - Article dateModified}};
if(dateModified){
data.dateModified = dateModified;
} else {
data.dateModified = {{SCHEMA - Article datePublished}};
}
var script = document.createElement('script');
script.type = "application/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTagName('head')[0].appendChild(script);
})(document);
</script>
此代码很好用,但不适用于dateModified。
有一个简单的代码,当dateModified不存在但不起作用时,会为此字段分配一个值。
现在,如果dateModified为null,则代码发送给google null值,从而导致架构数据出错。