php - Create dynamic HTML ID based off wordpress query -
i wondering how can create dynamic set of ids based off amount of custom posts queried given post.
i'm using advance custom fields plugin , i"m querying custom fields in given post. if take below you'll see have custom fields being queried each 1 wrapped in div id "section-1". need "section-1" update "section-3", "section-4" each time new field name queried. if 5 fields queried each have own id.
<?php // check if repeater field has rows of data if( have_rows('repeater_field_name') ): // loop through rows of data while ( have_rows('repeater_field_name') ) : the_row(); // display sub field value <div id="section-1"> the_sub_field('sub_field_name'); </div> endwhile; else : // no rows found endif; ?>
just set index
variable before loop , increment @ each iteration. use inside 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; ?>
Comments
Post a Comment