问题
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 theisSimilarTo
property), and anid
that gets referenced by theitemref
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