[英]OpenCart 2.3 Product Review Schema JSON-LD For Each Loop?
我不是真正的编码员,我破产了,试图在网上商店赚钱。 我正在使用OpenCart 2.3.0.2开一个在线商店,并一直在尝试SEO在启动之前对其进行优化。 这次,我试图通过结构化数据标记对其进行改进。 由于我目前没有足够的资金购买模块,因此我自己动手做并学习东西的工作原理。
我正在尝试为OpenCart 2.3创建自己的产品模块架构,但是卡在“评论”部分。 JSON-LD已完全构建并经过测试。
我正在尝试使用JSON-LD进行标记,是从此页面和OpenCart论坛中学到的, 这是有问题的代码的片段(复习部分):
"review": [
<?php foreach($reviewss as $review) { ?>
{
"@type": "Review",
"author": "<?php echo $review['author'];?>",
"datePublished": "<?php echo $review['date_added'];?>",
"description": "<?php echo $review['text'];?>",
"name": "<?php echo $review['author'];?>",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "<?php echo $review['rating'];?>",
"worstRating": "1"
}
}
<?php } ?>
]
}
产生输出:
"review": [
{
"@type": "Review",
"author": "A Parker",
"datePublished": "16/12/2018",
"description": "Wow! Product arrive yesterday and it's well packed. The product is well-designed.",
"name": "A Parker",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}
} #there should be a comma here after "}".
{
"@type": "Review",
"author": "David Lay",
"datePublished": "15/12/2018",
"description": "Great product! Works as advertised.",
"name": "David Lay",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}
}
]
}
Here's the correct one. Tested using Google Structured Data Markup.
"review": [
{
"@type": "Review",
"author": "A Parker",
"datePublished": "16/12/2018",
"description": "Wow! Product arrive yesterday and it's well packed. The product is well-designed.",
"name": "A Parker",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}
},
{
"@type": "Review",
"author": "David Lay",
"datePublished": "15/12/2018",
"description": "Great product! Works as advertised.",
"name": "David Lay",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "5",
"worstRating": "1"
}
}
]
}
我不确定它到底叫什么,我认为-LOOP问题。 对于每个评论帖子,应使用逗号分隔该评论。 如果只有1条评论,则代码工作正常。
应该有条件插入逗号,例如,如果审阅数超过一个,则插入逗号,否则,则没有逗号。 然后,当然,最后一项应该没有逗号。
我一直在努力解决这个问题。 我不了解PHP,但是我在论坛上做了很多努力,讨论了循环以及if / then条件, foreach
等问题,但是几乎听不懂。
我不确定如何问这个问题,也许是如何在OpenCart 2.3中针对JSON-LD架构循环产品评论? 任何帮助是极大的赞赏。
为什么不使用json_encode()
?
JSON-LD
产品架构看起来完全不同:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "Product",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": "3.5",
"reviewCount": "11"
},
"description": "0.7 cubic feet countertop microwave. Has six preset cooking categories and convenience features like Add-A-Minute and Child Lock.",
"name": "Kenmore White 17\" Microwave",
"image": "kenmore-microwave-17in.jpg",
"offers": {
"@type": "Offer",
"availability": "http://schema.org/InStock",
"price": "55.00",
"priceCurrency": "USD"
},
"review": [{
"@type": "Review",
"author": "Ellie",
"datePublished": "2011-04-01",
"description": "The lamp burned out and now I have to replace it.",
"name": "Not a happy camper",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "1",
"worstRating": "1"
}
}, {
"@type": "Review",
"author": "Lucas",
"datePublished": "2011-03-25",
"description": "Great microwave for the price. It is small and fits in my apartment.",
"name": "Value purchase",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "4",
"worstRating": "1"
}
}]
}
</script>
例如:
$data = (object) array(
"@context" => "http://schema.org",
"@type" => "Product",
"aggregateRating" => (object) array(
"@type": "AggregateRating",
"ratingValue" => "3.5",
"reviewCount" => "11"
),
"description" => "0.7 cubic feet countertop microwave. Has six preset cooking categories and convenience features like Add-A-Minute and Child Lock.",
"name" => "Kenmore White 17\" Microwave",
"image"=> "kenmore-microwave-17in.jpg",
"offers" => (object) array(
"@type": "Offer",
"availability": "http://schema.org/InStock",
"price": "55.00",
"priceCurrency": "USD"
),
"review" => array()
);
foreach($reviews as $review) {
array_push($data->review, $review);
}
echo '<script type="application/ld+json">';
echo json_encode($data);
echo '</script>';
另请参阅结构化数据测试工具 。
尝试这个
"review": [
<?php foreach($reviews as $key => $review); { ?>
{
"@type": "Review",
"author": "<?php echo $review['author'];?>",
"datePublished": "<?php echo $review['date_added'];?>",
"description": "<?php echo $review['text'];?>",
"name": "<?php echo $review['author'];?>",
"reviewRating": {
"@type": "Rating",
"bestRating": "5",
"ratingValue": "<?php echo $review['rating'];?>",
"worstRating": "1"
}
}
<?php if ($key != (count($reviews) - 1)){ ?>,<?php } ?><?php } ?>
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.