【问题标题】:OpenCart 2.3 Product Review Schema JSON-LD For Each Loop?每个循环的 OpenCart 2.3 产品评论模式 JSON-LD?
【发布时间】:2018-12-27 10:02:41
【问题描述】:

我并不是真正的程序员,我破产了,并试图通过在线商店赚钱。我正在使用 OpenCart 2.3.0.2 开设在线商店,并在启动前尝试对其进行 SEO 优化。这一次,我试图用结构化数据标记来改进它。由于我没有(目前)购买模块的经济能力,所以我自己做并学习东西是如何工作的。

我正在尝试为 OpenCart 2.3 的 Product 模块创建自己的架构,但在 Review 部分卡住了。 JSON-LD 是完全构建和测试的。

我尝试使用 JSON-LD 进行标记,从 this page 和 OpenCart 论坛了解到,这是有问题的代码的 sn-p(审查部分):

"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 条评论,则代码可以正常工作。

应该有插入逗号的条件,比如如果review超过1则插入逗号,如果没有则不插入逗号。那么当然,最后一项不应该有逗号。

我一直在努力解决这个问题。我不懂PHP,但我通过阅读论坛努力尝试,关于循环和if/then条件,foreach等主题。但几乎无法理解。

我不知道怎么问这个问题,也许如何在 OpenCart 2.3 中为 JSON-LD Schema 循环产品评论帖子? 非常感谢任何帮助。

【问题讨论】:

    标签: php jsonschema opencart2.3


    【解决方案1】:

    为什么不使用json_encode()

    JSON-LD Product 架构看起来完全不同:

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

    另见structured data testing tool

    【讨论】:

    • 谢谢!但是原始代码有效,唯一的问题是当有多个评论帖子时。另外,我不知道如何实施您上面的建议。我已经完全构建了上面提到的 json-ld 部分,我认为它只需要comma。您的代码可以与 OC 2.3 一起使用吗?
    • @JuliePera 您提供的代码无效且无法解析(不符合要求的格式,因此无法正常工作)。我的示例产生有效输出并考虑添加评论。显然你需要用值替换字符串。
    • 我发布的代码只是从完全构建的 json-ld 文件中提取的一个 sn-p。我在上面提到过。我只贴出了代码有问题的部分。
    【解决方案2】:

    试试这个

    "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 } ?>
    ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-30
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多