将简单的计数器变量添加到我的 ACF Repeater 循环

Adding simple counter variable to my ACF Repeater loop

我正在尝试将一个简单的计数器变量添加到手风琴的 ACF Repeater 循环中,但出现错误。谁能提供一些帮助?谢谢!

<?php
$counter = 0; if( have_rows('faq') ):$counter++; ?>    

                    <section class="ac-container">

                        <?php while( have_rows('faq') ): the_row(); 

                            $question = get_sub_field('faq_question');
                            $answer = get_sub_field('faq_answer');
                            ?>    
                                <div>
                                    <input id="ac-<?php echo $counter;?>" name="accordion-1" type="radio" checked />
                                    <label for="ac-<?php echo $counter;?>"><?php echo $question; ?></label>
                                    <article class="ac-small"><?php echo $answer; ?></article>
                                </div>    

                        <?php endwhile; ?>    
                    </section>    
                <?php endif; ?>

您的 $counter 变量不在您的循环中,因此不会随着每次迭代而增加。试试这个:

<?php
$counter = 0; if( have_rows('faq') ): ?>


<section class="ac-container">

    <?php while( have_rows('faq') ): the_row(); 

        $counter++; // this is now in the while loop
        $question = get_sub_field('faq_question');
        $answer = get_sub_field('faq_answer');
        ?>

        <div>
            <input id="ac-<?php echo $counter;?>" name="accordion-1" type="radio" checked />
            <label for="ac-<?php echo $counter;?>"><?php echo $question; ?></label>
            <article class="ac-small"><?php echo $answer; ?></article>
        </div>


    <?php endwhile; ?>

</section>

<?php endif; ?>

$counter 没有增加,因为你没有在 while 循环中增加它。在 while 循环中增加看起来像这样,

<?php
$counter = 0; if( have_rows('faq') ): ?>


                <section class="ac-container">

                    <?php while( have_rows('faq') ): the_row(); 

                        $question = get_sub_field('faq_question');
                        $answer = get_sub_field('faq_answer');
                        $counter++;    // Increment it inside while loop.
                        ?>
                            <div>
                                <input id="ac-<?php echo $counter;?>" name="accordion-1" type="radio" checked />
                                <label for="ac-<?php echo $counter;?>"><?php echo $question; ?></label>
                                <article class="ac-small"><?php echo $answer; ?></article>
                            </div>


                    <?php endwhile; ?>

                </section>

            <?php endif; ?>

这样试试

<?php
        if( have_rows('faq') ):$counter = 0;?>
            <section class="ac-container">
                <?php while( have_rows('faq') ): the_row();
                    $question = get_sub_field('faq_question');
                    $answer = get_sub_field('faq_answer');
                    ?>
                    <div>
                        <input id="ac-<?php echo $counter;?>" name="accordion-1" type="radio" checked />
                        <label for="ac-<?php echo $counter;?>"><?php echo $question; ?></label>
                        <article class="ac-small"><?php echo $answer; ?></article>
                    </div>
                    <?php $counter++; endwhile; ?>

            </section>
        <?php endif; ?>