ACF Flexible Field 中的循环计数器
Loop counter within ACF Flexible Field
我正在使用高级自定义字段 (ACF),并且我有一个灵活的内容部分设置。
我正在尝试循环计数器,这样我就可以为每个选项卡部分添加一个唯一的 ID。
这是我的代码和我尝试过的代码。我试图在程序 accordion div 中添加一个外部循环,以便在没有太多运气的情况下循环计数器编号。
elseif ( get_row_layout() == 'program_accordion') : $counter = 0;
while ( has_sub_field('program_accordion') ) :
$counter++; ?>
<div class="program-accordion">
<input id="tab-<?php echo $counter ?>" type="checkbox" name="tabs">
<label for="tab-"><?php the_sub_field('accordion_title'); ?></label>
<div class="tab-content">
<?php the_sub_field('program_description_accordion');
if( have_rows('accordion_speaker_fields')) :
while( have_rows('accordion_speaker_fields')) : the_row(); ?>
<img src="<?php the_sub_field('accordion_image'); ?>">
<?php the_sub_field('accordion_lightbox_content'); ?>
<?php
endwhile;
endif; ?>
</div>
</div>
<?php
endwhile;
endif;
我一直在尝试 运行 通过此处的文档:https://www.advancedcustomfields.com/resources/has_sub_field/ and here: https://support.advancedcustomfields.com/forums/topic/unique-ids-for-each-layout-row/ 但我只是碰壁了。
我想知道这是否与我正在调用的字段有关,以及这些字段是否准确,因为没有显示任何内容。我在 ACF 中的字段已按照下图设置。
如有任何帮助,我们将不胜感激。
问题出在您的 while 循环上。
while(has_sub_field('program_accordion'))
循环不执行任何操作,因为 program_accordion 只是布局名称,因此没有子字段。
您的字段结构意味着每个 program_accordion 都有一个选项卡,所以我假设您希望计数器对所有选项卡递增,而不仅仅是每个 program_accordion 中的选项卡(因为它们会在这种情况下始终为 1)。因此,您还需要将计数器初始化移到主 while 循环之外,这样您就不会为每个选项卡重置它。
(如果您遇到问题,打印出所有 ACF 字段以更清楚地查看结构会有所帮助,即 print_r(get_fields());
)
基于该假设,以下将增加每个选项卡的计数器:
$counter = 0;
// I presume this is your outer loop...
if( get_field('description_container') ):
while( has_sub_field("description_container") ):
if (/* whatever condition you had here */):
elseif ( get_row_layout() == 'program_accordion') :
// REMOVE THIS LOOP:
// while ( has_sub_field('program_accordion') ) :
$counter++; ?>
<div class="program-accordion">
// display accordion content here
// FYI, you're missing the $counter var from your label
</div>
<?php
// endwhile; - of removed loop
endif;
endwhile;
endif;
?>
我正在使用高级自定义字段 (ACF),并且我有一个灵活的内容部分设置。
我正在尝试循环计数器,这样我就可以为每个选项卡部分添加一个唯一的 ID。
这是我的代码和我尝试过的代码。我试图在程序 accordion div 中添加一个外部循环,以便在没有太多运气的情况下循环计数器编号。
elseif ( get_row_layout() == 'program_accordion') : $counter = 0;
while ( has_sub_field('program_accordion') ) :
$counter++; ?>
<div class="program-accordion">
<input id="tab-<?php echo $counter ?>" type="checkbox" name="tabs">
<label for="tab-"><?php the_sub_field('accordion_title'); ?></label>
<div class="tab-content">
<?php the_sub_field('program_description_accordion');
if( have_rows('accordion_speaker_fields')) :
while( have_rows('accordion_speaker_fields')) : the_row(); ?>
<img src="<?php the_sub_field('accordion_image'); ?>">
<?php the_sub_field('accordion_lightbox_content'); ?>
<?php
endwhile;
endif; ?>
</div>
</div>
<?php
endwhile;
endif;
我一直在尝试 运行 通过此处的文档:https://www.advancedcustomfields.com/resources/has_sub_field/ and here: https://support.advancedcustomfields.com/forums/topic/unique-ids-for-each-layout-row/ 但我只是碰壁了。
我想知道这是否与我正在调用的字段有关,以及这些字段是否准确,因为没有显示任何内容。我在 ACF 中的字段已按照下图设置。
如有任何帮助,我们将不胜感激。
问题出在您的 while 循环上。
while(has_sub_field('program_accordion'))
循环不执行任何操作,因为 program_accordion 只是布局名称,因此没有子字段。
您的字段结构意味着每个 program_accordion 都有一个选项卡,所以我假设您希望计数器对所有选项卡递增,而不仅仅是每个 program_accordion 中的选项卡(因为它们会在这种情况下始终为 1)。因此,您还需要将计数器初始化移到主 while 循环之外,这样您就不会为每个选项卡重置它。
(如果您遇到问题,打印出所有 ACF 字段以更清楚地查看结构会有所帮助,即 print_r(get_fields());
)
基于该假设,以下将增加每个选项卡的计数器:
$counter = 0;
// I presume this is your outer loop...
if( get_field('description_container') ):
while( has_sub_field("description_container") ):
if (/* whatever condition you had here */):
elseif ( get_row_layout() == 'program_accordion') :
// REMOVE THIS LOOP:
// while ( has_sub_field('program_accordion') ) :
$counter++; ?>
<div class="program-accordion">
// display accordion content here
// FYI, you're missing the $counter var from your label
</div>
<?php
// endwhile; - of removed loop
endif;
endwhile;
endif;
?>