简体   繁体   English

在带有 PHP WordPress 循环的 Schema 数组中的最后一个 object 之后删除逗号

[英]Remove comma after last object in the Schema array with PHP WordPress Loop

I have a "Product" Google Schema Markup with the pasted loop for the "Reviews".我有一个“产品”谷歌架构标记,其中包含“评论”的粘贴循环。 Here's a part of the Markup's code:这是标记代码的一部分:

     "review": [
            <?php
            $args = array(
            'post_type' => 'my_reviews',
            'category_name' => 'my-product', 
            'paged' => $paged);
    
            $loop = new WP_Query($args);
            if ($loop->have_posts()) :
            while ($loop->have_posts()) : $loop->the_post(); ?>
{
            
            "@type": "Review",
            "reviewRating": {
              "@type": "Rating",
              "ratingValue": "5"
            },
            "author": {
              "@type": "Person",
              "name": "<?php the_title(); ?>"
            },
            "reviewBody": "<?php echo get_the_content(); ?>"},
    <?php
    endwhile;
    endif;
    wp_reset_postdata();
    ?>],
    
          "aggregateRating": {
            "@type": "AggregateRating",
            "ratingValue": "5",
            "bestRating": "5",
            "ratingCount": "<?php echo count_cat_post('My Product'); ?>"
},

Everything works as it should, except that after } of the last object, a comma is still trapped.一切正常,除了在最后一个}之后,逗号仍然被困住。 And it turns out something like the following:结果是这样的:

   "review": [{
        "@type": "Review",
        "reviewRating": {
          "@type": "Rating",
          "ratingValue": "5"
        },
        "author": {
          "@type": "Person",
          "name": "John Doe"
        },
        "reviewBody": "Review 1 Content"
      },
      {
        "@type": "Review",
        "reviewRating": {
          "@type": "Rating",
          "ratingValue": "1"
        },
        "author": {
          "@type": "Person",
          "name": "Jane Doe"
        },
        "reviewBody": "Review 2 Content."
      }, <-- this is the comma I need to remove
],
      "aggregateRating": {
        "@type": "AggregateRating",
        "ratingValue": "88",
        "bestRating": "100",
        "ratingCount": "2"
      },

How can I remove it?我怎样才能删除它?

in wordpress loop you have property current_post for index and post_count for total number of posts .在 wordpress 循环中,您拥有用于index的属性current_post和用于total number of posts post_count You can use condition ($loop->current_post + 1 != $loop->post_count) to compare that it is not a last post then you can print comma.您可以使用条件($loop->current_post + 1 != $loop->post_count)来比较它不是最后一篇文章,然后您可以打印逗号。

so your code for get_the_content should be like this:所以你的get_the_content代码应该是这样的:

"reviewBody": "<?php echo get_the_content(); ?>"} <?php if ($loop->current_post + 1,= $loop->post_count) { echo ';'? } ?>

You hould not be creating JSON by concatenating with a loop, instead within the loop you should be adding your text to an array, and then use implode(',',$yourArray) to convert the array to a JSON, where that last comma would not exist.您不应该通过与循环连接来创建 JSON,而是在循环内将文本添加到数组中,然后使用implode(',',$yourArray)将数组转换为 JSON,最后一个逗号将不存在。

Or even better approach is to create nested arrays, and use json_encode() function to create a valid JSON out of a nested array.甚至更好的方法是创建嵌套的 arrays,并使用json_encode() function 从嵌套数组中创建有效的 JSON。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM