在除第一个元素之外的每个元素之前添加一个逗号

有一个循环:


<?php if ( have_rows( 'fifth-section' ) ) : ?>

    <?php while ( have_rows( 'fifth-section' ) ) : the_row(); ?>

    <?php if ( get_sub_field( 'display' ) == 1 ) { ?>

      "offers": {

        "@type": "AggregateOffer",

        "offers": [

        <?php if( have_rows('plans') ): ?>

        <?php 

          $loop_counter = 0;

          while( have_rows('plans') ): the_row(); 

          if($loop_counter%4 == 0){

            if($loop_counter > 0){ 

            ?>

          <?php } ?>

          <?php }

          $subtitle = get_sub_field('subtitle');

        ?>                          

        {

          "@type": "Offer",

          "name": "<?php echo $subtitle; ?>",

          "url": "<?php echo get_permalink(); ?>"

        },

        <?php 

          $loop_counter++;

          endwhile;

        ?>

        <?php endif; ?>

      <?php } else { } ?>

      <?php endwhile; ?>

      <?php endif; ?>

      ]

    }

  }

哪个输出模式标记的块


"offers": {

      "@type": "AggregateOffer",

      "offers": [                                                       

        {

          "@type": "Offer",

          "name": "title",

          "url": "example.com/assets/1"

        },


        {

          "@type": "Offer",

          "name": "title 2",

          "url": "example.com/assets/2"

        },

      ]

    }

  }

问题是右括号 } 之后的最后一个 Offer 元素不应该有逗号。元素可以是 1 到 4。实际上一个问题是如何将除最后一个元素之外的所有元素注册为逗号(当然,如果项目为 1,则不要添加)?


炎炎设计
浏览 131回答 1
1回答

LEATH

除了第一个元素之外,在每个元素之前添加一个逗号就足够了。尝试添加:<?php if ($loop_counter > 0) echo ',' ?>像这样的东西:&nbsp; &nbsp; &nbsp; &nbsp; <?php if ( have_rows( 'fifth-section' ) ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php while ( have_rows( 'fifth-section' ) ) : the_row(); ?>&nbsp; &nbsp; &nbsp; &nbsp; <?php if ( get_sub_field( 'display' ) == 1 ) { ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "offers": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "@type": "AggregateOffer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "offers": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if( have_rows('tariffs') ): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $loop_counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while( have_rows('tariffs') ): the_row();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($loop_counter%4 == 0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($loop_counter > 0){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tariffs_subtitle = get_sub_field('subtitle');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $tariffs_cost = get_sub_field('cost');&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ($loop_counter > 0) echo ',' ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "@type": "Offer",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "name": "<?php echo $tariffs_subtitle; ?>",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "url": "<?php echo get_permalink(); ?>#price",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "price": "<?php echo $tariffs_cost; ?>",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "priceCurrency": "RUB"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $loop_counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } else { } ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP