简体   繁体   中英

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 . 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.

so your code for get_the_content should be like this:

"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.

Or even better approach is to create nested arrays, and use json_encode() function to create a valid JSON out of a nested array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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