简体   繁体   English

如何使用 Microdata 的“itemref”来引用产品 scope 之外列出的类似产品?

[英]How to use Microdata's 'itemref' to reference similar products that are listed outside of the Product scope?

I am trying to use Microdata to define my website using the Schema.org definitions.我正在尝试使用 Microdata 使用 Schema.org 定义来定义我的网站。 On an ItemPage I am displaying the information about a product.ItemPage上,我正在显示有关产品的信息。 Additionally, I want to link similar products to the current product while the related product is being displayed outside of the current product's scope.此外,我想在当前产品的 scope 之外显示相关产品时将类似产品链接到当前产品

I tried to achieve this using the itemref attribute.我尝试使用itemref属性来实现这一点。 However, when I review the page on Structured Data Testing Tool it does not show the related products are part of the ItemPage node.但是,当我查看结构化数据测试工具上的页面时,它并没有显示相关产品是ItemPage节点的一部分。

<body itemscope itemtype="http://schema.org/ItemPage">
  <header itemprop="hasPart" itemscope itemtype="http://schema.org/WPHeader">
  </header>

  <article itemprop="mainEntity" itemscope itemtype="http://schema.org/Product" id="details_10">
    <h2 itemprop="name">Product 10</h2>
  </article>

  <aside>
    <article itemref="details_10" itemscope itemtype="http://schema.org/Product">
      <h3 itemprop="name">Product 20</h3>
    </article>

    <article itemref="details_10" itemscope itemtype="http://schema.org/Product">
      <h3 itemprop="name">Product 30</h3>
    </article>
  </aside>

  <footer itemprop="hasPart" itemscope itemtype="http://schema.org/WPFooter">
  </footer>

</body>

It has to be used the other way around:它必须以相反的方式使用:

  • The itemref attribute needs to be on the element that represents the item you want to add properties to. itemref属性需要位于表示要添加属性的项目的元素上。 This would be the primary product in your case.这将是您的主要产品。

  • The elements you want to add need the itemprop attribute (which you are missing, along with the isSimilarTo property), and an id that gets referenced by the itemref attribute.您要添加的元素需要itemprop属性(您缺少该属性以及isSimilarTo属性),以及由itemref属性引用的id These would be the similar products in your case.这些将是您的类似产品。

So, this would give:因此,这将给出:

<body itemscope itemtype="http://schema.org/ItemPage">

 <article itemprop="mainEntity" itemscope itemtype="http://schema.org/Product" itemref="details_20 details_30"></article>

 <aside>
   <article id="details_20" itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product"></article>
   <article id="details_30" itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product"></article>
 </aside>

</body>

The problem with this markup is that the two isSimilarTo properties get added to the ItemPage , too (because they are nested under it), which would be incorrect.这个标记的问题是两个isSimilarTo属性也被添加到ItemPage (因为它们嵌套在它下面),这是不正确的。 To avoid this, the best solution would be not to specify the ItemPage on the body element, but on a div or similar.为避免这种情况,最好的解决方案不是在body元素上指定ItemPage ,而是在div或类似元素上指定。

<body>

 <div itemscope itemtype="http://schema.org/ItemPage">

   <article itemprop="mainEntity" itemscope itemtype="http://schema.org/Product" itemref="details_20 details_30"></article>

 </div>

 <aside>
   <article id="details_20" itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product"></article>
   <article id="details_30" itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product"></article>
 </aside>

</body>

(You could also use itemref to avoid having to nest the primary Product under the ItemPage . This would also allow to specify the ItemPage on the head element, for example.) (您也可以使用itemref来避免将主要Product嵌套在ItemPage下。例如,这也允许在head元素上指定ItemPage 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM