Php 基于wordpress查询创建动态HTML ID
我想知道如何根据为给定帖子查询的自定义帖子数量创建一组动态ID 我正在使用Advance Custom Fields插件,然后我正在查询给定帖子中的自定义字段。如果您查看下面的内容,您会看到我查询了我的自定义字段,每个字段都包装在一个id为“section-1”的div中。我需要的是“section-1”更新为“section-3”、“section-4”“每次查询新字段名时。因此,如果查询5个字段,每个字段都有自己的IDPhp 基于wordpress查询创建动态HTML ID,php,html,wordpress,Php,Html,Wordpress,我想知道如何根据为给定帖子查询的自定义帖子数量创建一组动态ID 我正在使用Advance Custom Fields插件,然后我正在查询给定帖子中的自定义字段。如果您查看下面的内容,您会看到我查询了我的自定义字段,每个字段都包装在一个id为“section-1”的div中。我需要的是“section-1”更新为“section-3”、“section-4”“每次查询新字段名时。因此,如果查询5个字段,每个字段都有自己的ID <?php // check if the repeater f
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-1">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>
只需在循环之前设置一个
索引
变量,并在每次迭代时递增它。在id
中使用它
<?php
$index = 1;
while ( have_rows('repeater_field_name') ) : the_row(); ?>
<div id="section-<?= $index; ?>">
<?php the_sub_field('sub_field_name'); ?>
</div>
<?php $index++
endwhile; ?>
试试这个
<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
$i = 0;
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// display a sub field value
<div id="section-<?php echo ++$i; ?>">
the_sub_field('sub_field_name');
</div>
endwhile;
else :
// no rows found
endif;
?>