【问题标题】:Using structured data markup javascript使用结构化数据标记 javascript
【发布时间】:2021-12-02 21:15:06
【问题描述】:

在结构化数据标记中,我想用 java 脚本插入发布日期。有没有办法做到这一点?

示例架构;

<script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "NewsArticle",
      "headline": "Article headline",
      "image": [
        "https://example.com/photos/1x1/photo.jpg",
        "https://example.com/photos/4x3/photo.jpg",
        "https://example.com/photos/16x9/photo.jpg"
       ],
      "datePublished": "+ date variable +",
      
      "author": [{
          "@type": "Person",
          "name": "Jane Doe",
          "url": "http://example.com/profile/janedoe123"
        },{
          "@type": "Person",
          "name": "John Doe",
          "url": "http://example.com/profile/johndoe123"
      }]
    }
    </script>

我必须在页面上的时间标签中获取日期并使用 javascript 将其放入架构中。

<time datetime="2021-02-24T12:11:00+03:00">2021-02-24T12:11:00+03:00</time>

【问题讨论】:

  • 为什么不用&lt;data:post.date/&gt; 而不是+ date variable +
  • 我正在将它添加到帖子内容中,数据代码在这里不起作用。

标签: javascript schema blogger json-ld


【解决方案1】:

我删除了datePublished,因为如果有JS变量它不是有效的标记,我们应该完全使用JS添加它,即使禁用了Javascript也是有效的。

<script id='structured-data' type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "NewsArticle",
  "headline": "Article headline",
  "image": ["https://example.com/photos/1x1/photo.jpg", "https://example.com/photos/4x3/photo.jpg", "https://example.com/photos/16x9/photo.jpg"],
  "author": [{
      "@type": "Person",
      "name": "Jane Doe",
      "url": "http://example.com/profile/janedoe123"
    },{
      "@type": "Person",
      "name": "John Doe",
      "url": "http://example.com/profile/johndoe123"
    }]
}
</script>

添加datePublished可以使用这个

let timeValue = document.querySelector('time').dateTime;
let structuredData = document.getElementById('structured-data');
let parsedSD = JSON.parse(structuredData.innerText);
parsedSD.datePublished = timeValue;
structuredData.innerText = JSON.stringify(parsedSD)

【讨论】:

  • 完美运行,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 2015-04-17
  • 2023-03-21
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多