Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WordPress ACF Repeater PHP获取\行\索引获取最后一行而不是第一行(删除逗号JSON-LD)_Php_Wordpress_Schema_Advanced Custom Fields_Json Ld - Fatal编程技术网

WordPress ACF Repeater PHP获取\行\索引获取最后一行而不是第一行(删除逗号JSON-LD)

WordPress ACF Repeater PHP获取\行\索引获取最后一行而不是第一行(删除逗号JSON-LD),php,wordpress,schema,advanced-custom-fields,json-ld,Php,Wordpress,Schema,Advanced Custom Fields,Json Ld,我正在使用WordPress ACF插件并设置一个JSON LD转发器,这是非常基本的。但是,我无法验证JSON-LD代码,因为转发器在每个转发器中添加了一个逗号,这会使代码无效,因为JSON-LD脚本的最后一个对象不能使用公共名称 以下是我使用的代码: "review": [ <?php if(have_rows('customer_reviews_repeater')): ?> <?php while(have_rows('customer_

我正在使用WordPress ACF插件并设置一个JSON LD转发器,这是非常基本的。但是,我无法验证JSON-LD代码,因为转发器在每个转发器中添加了一个逗号,这会使代码无效,因为JSON-LD脚本的最后一个对象不能使用公共名称

以下是我使用的代码:

  "review": [
  <?php if(have_rows('customer_reviews_repeater')): ?>
  <?php while(have_rows('customer_reviews_repeater')): the_row(); ?>
    {
      "@type": "Review",
      "author": "<?php the_sub_field('customer_name'); ?>",
      "datePublished": "<?php the_sub_field('date_hidden'); ?>",
      "description": "<?php the_sub_field('review_body'); ?>",
      "reviewRating": {
        "@type": "Rating",
        "ratingValue": "<?php the_sub_field('review_value'); ?>"
      }
    }, <!-- // This comma should not appear on the last ROW -->
  <?php endwhile; ?>
  <?php endif; ?> 
  ],

您可以只计算返回的行数,并创建一个计数器,将该计数器用于逗号条件<代码>计数(get_字段(“customer_reviews_repeater”)将返回从0开始的行数。请记住检查字段是否为数组,否则计数将抛出错误
  <?php if(have_rows('customer_reviews_repeater')): ?>
  <?php while(have_rows('customer_reviews_repeater')): the_row(); ?>
    {
      "@type": "Review",
      "author": "<?php the_sub_field('customer_name'); ?>",
      "datePublished": "<?php the_sub_field('date_hidden'); ?>",
      "description": "<?php the_sub_field('review_body'); ?>",
      "reviewRating": {
        "@type": "Rating",
        "ratingValue": "<?php the_sub_field('review_value'); ?>"
      }
    }<?php if( get_row_index() != 1 ){ echo ','; } ?>
  <?php endwhile; ?>
  <?php endif; ?>