WordPress ACF Repeater PHP获取\行\索引获取最后一行而不是第一行(删除逗号JSON-LD)
我正在使用WordPress ACF插件并设置一个JSON LD转发器,这是非常基本的。但是,我无法验证JSON-LD代码,因为转发器在每个转发器中添加了一个逗号,这会使代码无效,因为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_
"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; ?>