-1

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?

10
  • If you remove that comma, it makes the JSON invalid! Commented Nov 25, 2021 at 22:41
  • @Dula thank you for your message, but could you, please, look more attentively. The comma I mentioned comes after the figure bracket before the closing square bracket of the array. Commented Nov 25, 2021 at 22:44
  • Ah..sorry about that! I was not looking closely. But why do you want to remove that comma, as it will not affect anything? Commented Nov 25, 2021 at 22:48
  • 1
    you can put some condition like this "reviewBody": "<?php echo get_the_content(); ?>"} <?php if ($loop->current_post + 1 != $loop->post_count) { echo ','; } ?> Commented Nov 25, 2021 at 22:58
  • 1
    Yes, @Claymore solution is what I meant, there are better solutions which you should explore when you have the chance ;) Commented Nov 26, 2021 at 7:21

2 Answers 2

1

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 ','; } ?>

Update for all others : I know json_encode is correct way but he said in comment that he want it like this. But for future viewers, Correct approach should be like this :

// define reviews array
   $reviewArr = [
       'review' => [],
       'aggregateRating' => []
   ];

   // get and loop through posts
    $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();

        // new post review
        $post_review = [
            
            "@type" => "Review",
            "reviewRating" => [
              "@type" => "Rating",
              "ratingValue" => "5"
            ],
            "author" => [
              "@type" => "Person",
              "name" => "<?php the_title(); ?>"
            ],
            "reviewBody" => get_the_content()
        ];

        // insert the post review in reviews array
        $reviewArr['review'][] = $post_review;

    endwhile;
    endif;
    wp_reset_postdata();

    // aggregate rating
    $aggRating =  [
        "@type" => "AggregateRating",
        "ratingValue" => "5",
        "bestRating" => "5",
        "ratingCount" => count_cat_post('My Product')
    ];

    // insert in reviews array
    $reviewArr['aggregateRating'] = $aggRating;


    //  here you get your json
    $json = json_encode($reviewArr);
Sign up to request clarification or add additional context in comments.

3 Comments

I must warn all future researchers that this technique MUST NOT be used in professional/stable applications. It is very bad practice to manually craft json strings. PHP natively offers json_encode() and that is easily the best way to proceed with this task. You should be gathering all of your data, then when you are done building/manipulating it, encode it. stackoverflow.com/questions/38513620/…
this is NOT the correct way to solve the issue
@mickmackus I know json_encode is correct way but he wanted to manually create json data, updated my answer for the json_encode way too.
0

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.

1 Comment

@Morgari volunteers in Stack Overflow have been saying this same thing for years and years. This answer is giving the correct/professional advice. Don't craft you json strings manually. Please rethink the way that you are coding your application.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.