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

不打扰是莪最后的温柔 提交于 2020-01-24 19:59:08

问题


I am trying to use Microdata to define my website using the Schema.org definitions. On an ItemPage I am displaying the information about a product. 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.

I tried to achieve this using the itemref attribute. However, when I review the page on Structured Data Testing Tool it does not show the related products are part of the ItemPage node.

<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>

回答1:


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. 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. 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. To avoid this, the best solution would be not to specify the ItemPage on the body element, but on a div or similar.

<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.)



来源:https://stackoverflow.com/questions/58273988/how-to-use-microdatas-itemref-to-reference-similar-products-that-are-listed-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!