问题
I am trying to use Microdata to define my website using the Schema.org definitions.
Below is my current HTML markup:
<body itemscope itemtype="http://schema.org/ItemPage">
<link itemprop="url" href="https://example.com/i/10" />
<main role="main">
<!-- Show the main product of the page -->
<div itemprop="mainEntity" itemtype="http://schema.org/Product" itemscope>
<meta itemprop="name" content="My Main Product 10 Name" />
<!-- ... more properties that describes current product -->
</div>
<!-- List of 10 similar product the current product being viewed -->
<div class="list-related-products">
<div itemtype="http://schema.org/Product" itemscope>
<meta itemprop="name" content="Related Product 20 Name" />
<meta itemprop="url" content="https://example.com/i/20" />
<div itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product">
<link itemprop="url" href="https://example.com/i/10" />
</div>
<!-- ... more properties -->
</div>
<!-- ... more products -->
</div>
</main>
</body>
When I validate the code using Structured Data Testing Tool, the similar products section shows up as a separate nodes and not part of the ItemPage.
How can I list the related similar products correctly under the current product being defined?
回答1:
Solution 1: nesting
You can add isSimilarTo
inside the primary product:
<article itemprop="mainEntity" itemscope itemtype="http://schema.org/Product">
<h2 itemprop="name">Product 10</h2>
<aside>
<article itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product">
<h3 itemprop="name">Product 20</h3>
</article>
<article itemprop="isSimilarTo" itemscope itemtype="http://schema.org/Product">
<h3 itemprop="name">Product 30</h3>
</article>
</aside>
</article>
Solution 2: itemref
If you can’t nest the similar products under the HTML element for the primary product, you could use Microdata’s itemref
(example).
Solution 3: ID
(You should only go this way if solutions 1 or 2 aren’t possible, as not all consumers will support this solution 3.)
Similar to what you’re currently using, you can give the primary product an URI as identifier (with Microdata’s itemid
attribute) and reference this URI as value for the isSimilarTo
inside the similar products.
<article itemprop="mainEntity" itemscope itemtype="http://schema.org/Product" itemid="https://example.com/i/10#this">
<h2 itemprop="name">Product 10</h2>
</article>
<aside>
<article itemscope itemtype="http://schema.org/Product">
<h3 itemprop="name">Product 20</h3>
<link itemprop="isSimilarTo" href="https://example.com/i/10#this" />
</article>
<article itemscope itemtype="http://schema.org/Product">
<h3 itemprop="name">Product 30</h3>
<link itemprop="isSimilarTo" href="https://example.com/i/10#this" />
</article>
</aside>
来源:https://stackoverflow.com/questions/58261093/how-to-use-microdata-to-represent-similar-products-on-the-itempage