P粉8199374862023-09-06 00:12:16
Create a variable with the number you want to start with, e.g. 1, and then use PHP's post-increment $a
Increment as you loop through the next tab or content its value.
<ul class="nav nav-tabs" id="myTab" role="tablist"> <?php $tabIndex = 1; $tabs = get_field('tab'); if( $tabs ) : foreach( $tabs as $tab) : $headline = $tab['headline']; ?> <li class="nav-item" role="presentation"> <a class="nav-link<?php if ($tabIndex === 1) { echo ' active'; } ?>" id="home-tab" data-toggle="tab" data-target="#tab-<?php echo $tabIndex; ?>" type="button" role="tab" aria-controls="home" aria-selected="true"> <?php echo $headline; ?> </a> </li> <?php $tabIndex++; ?> <?php endforeach; ?> <?php endif; ?> </ul> <div class="tab-content page" id="myTabContent"> <?php $tabIndex = 1; $tabs = get_field('tab'); if( $tabs ) : foreach( $tabs as $tab) : $description = $tab['description']; ?> <div class="tab-pane fade show<?php if ($tabIndex === 1) { echo ' active'; } ?>" id="tab-<?php echo $tabIndex; ?>" role="tabpanel" aria-labelledby="tab-<?php echo $tabIndex; ?>"> <p> <?php echo $description; ?> </p> </div> <?php $tabIndex++; ?> <?php endforeach; ?> <?php endif; ?> </div>