所以我有一些代码正在吐出Repeater字段类型,只要我的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,以便我可以将其作为页面的一部分链接到它。
我试图自己做,但我创造了一个永远持续的循环...有人可以建议前进的道路吗?
答案 0 :(得分:0)
如果您需要为每个部分中的每个转发器行添加唯一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; ?>