如何创建为每个转发器行分配唯一 ID 的 PHP 计数器


How to create a PHP counter that assigns a unique id to each repeater row?

所以我有一些代码,只要我的 WP 后端中有这个字段的实例,就会吐出中继器字段类型。看起来像这样

<div class="custom-product-list">
  <div class="cpl-center">
  <?php
  // check if the repeater field has rows of data
  if( have_rows('custom_products') ):
    // loop through the rows of data
    while ( have_rows('custom_products') ) : the_row();
  ?>
     <div class="custom-product">
      <div class="col col_1">
        <div class="custom-mobile-image">
          <img src="<?php the_sub_field('product_image'); ?>" alt="<?php the_sub_field('product_name'); ?>" />
        </div><!-- .custom-mobile-image -->
        <h2><?php the_sub_field('product_name'); ?></h2>
        <p><?php the_sub_field('product_description'); ?></p>
        <h3>Benefits</h3>
        <?php
        // check if the repeater field has rows of data
        if( have_rows('benefits') ):
        ?>
          <ul>
        <?php
          // loop through the rows of data
          while ( have_rows('benefits') ) : the_row();
        ?>
          <li><?php the_sub_field('benefit'); ?></li>
        <?php
          endwhile;
        ?>
          </ul>
        <?php
        endif;
        ?>
      </div><!-- .col -->
      <div class="col col_2 last">
        <div class="custom-product-image">
          <img src="<?php the_sub_field('product_image'); ?>"alt="<?php the_sub_field('product_name'); ?>" />
        </div><!-- .custom-product-image -->
        <div class="col col_3">
          <h3>For Best Results</h3>
          <?php
          // check if the repeater field has rows of data
          if( have_rows('for_best_results') ):
          ?>
            <ul>
          <?php
            // loop through the rows of data
            while ( have_rows('for_best_results') ) : the_row();
          ?>
            <li><?php the_sub_field('result'); ?></li>
          <?php
            endwhile;
          ?>
            </ul>
          <?php
          endif;
          ?>
        </div><!-- .col -->
        <div class="col col_4 last">
          <h3>Type</h3>
          <?php
          // check if the repeater field has rows of data
          if( have_rows('type') ):
          ?>
            <ul>
          <?php
            // loop through the rows of data
            while ( have_rows('type') ) : the_row();
          ?>
            <li><?php the_sub_field('entry'); ?></li>
          <?php
            endwhile;
          ?>
            </ul>
          <?php
          endif;
          ?>
        </div><!-- .col -->
        <div class="clear"></div><!-- .clear -->
      </div><!-- .col -->
      <div class="clear"></div><!-- .clear -->
    </div><!-- .custom-product -->
  <?php
    endwhile;
  endif;
  ?>

我想添加一个计数器,该计数器将为中继器字段的每个新行或实例提供唯一 ID,以便我可以将其作为页面的一部分链接到它。

我试图自己做,但我创建了一个永远持续的循环......有人可以就前进的道路提供建议吗?

如果您需要为每个部分中的每个转发器行添加唯一 id,这样的东西可能适合您。显然,您必须为每个特定部分执行此操作

// check if the repeater field has rows of data
<?php if( have_rows('benefits') ): 
    //Introduce a counter for each section
    $benefits_sub_item=1;
?>
    <ul>
        <?php // loop through the rows of data
        while ( have_rows('benefits') ) : the_row();
        ?>
            <li id="benefit-item-<?php echo $benefits_sub_item;"><?php the_sub_field('benefit'); ?></li>
        <?php
            $benefits_sub_item++;
        endwhile;
        ?>
    </ul>
<?php endif; ?>