【问题标题】:Remove comma after last object in the Schema array with PHP WordPress Loop [duplicate]使用 PHP WordPress 循环删除 Schema 数组中最后一个对象后的逗号 [重复]
【发布时间】:2022-01-04 03:11:21
【问题描述】:

我有一个“产品”谷歌架构标记,其中包含“评论”的粘贴循环。 这是标记代码的一部分:

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

一切正常,除了在最后一个对象的} 之后,逗号仍然被困住。 结果是这样的:

   "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"
      },

我怎样才能删除它?

【问题讨论】:

  • 如果你删除那个逗号,它会使 JSON 无效!
  • @Dula 感谢您的留言,但请您仔细看一下。我提到的逗号位于数组右方括号之前的数字括号之后。
  • 啊..对不起!我没有仔细看。但是为什么要删除那个逗号,因为它不会影响任何东西?
  • 你可以设置这样的条件"reviewBody": "&lt;?php echo get_the_content(); ?&gt;"} &lt;?php if ($loop-&gt;current_post + 1 != $loop-&gt;post_count) { echo ','; } ?&gt;
  • 是的,@Claymore 解决方案就是我的意思,有更好的解决方案你应该在有机会的时候探索;)

标签: javascript php json


【解决方案1】:

您不应该通过连接循环来创建 JSON,而是在循环中将文本添加到数组中,然后使用 implode(',',$yourArray) 将数组转换为 JSON,其中最后一个逗号将不存在.

或者更好的方法是创建嵌套数组,并使用json_encode() 函数从嵌套数组中创建有效的 JSON。

【讨论】:

  • @Morgari Stack Overflow 的志愿者多年来一直在说同样的话。这个答案给出了正确/专业的建议。不要手动制作 json 字符串。请重新考虑编写应用程序的方式。
【解决方案2】:

在 wordpress 循环中,index 的属性为 current_posttotal number of posts 的属性为 post_count。您可以使用条件($loop-&gt;current_post + 1 != $loop-&gt;post_count)来比较它不是最后一个帖子,然后您可以打印逗号。

所以get_the_content 的代码应该是这样的:

"reviewBody": "&lt;?php echo get_the_content(); ?&gt;"} &lt;?php if ($loop-&gt;current_post + 1 != $loop-&gt;post_count) { echo ','; } ?&gt;

所有其他人的更新:我知道json_encode 是正确的方式,但他在评论中说他想要这样。但对于未来的观众来说,正确的做法应该是这样的:

// 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);

【讨论】:

  • 我必须警告所有未来的研究人员,该技术不得用于专业/稳定的应用程序。手动制作 json 字符串是非常糟糕的做法。 PHP 本身提供了json_encode(),这很容易成为完成这项任务的最佳方式。您应该收集所有数据,然后在完成构建/操作后对其进行编码。 stackoverflow.com/questions/38513620/…
  • 这不是解决问题的正确方法
  • @mickmackus 我知道 json_encode 是正确的方式,但他想手动创建 json 数据,也更新了我对 json_encode 方式的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 2019-10-04
  • 1970-01-01
  • 2018-02-14
  • 1970-01-01
相关资源
最近更新 更多