Lisa Jaffé
Lisa Jaffé

Reputation: 51

Adding simple counter variable to my ACF Repeater loop

I am trying to add a simple counter variable to my ACF Repeater loop for an accordion but am getting an error. Can anyone offer some assistance? Thanks!

<?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; ?>

Upvotes: 0

Views: 3402

Answers (3)

Narek Elbakyan
Narek Elbakyan

Reputation: 182

Try like this

<?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; ?>

Upvotes: 5

airdrumz
airdrumz

Reputation: 449

Your $counter variable is not inside your loop so won't increase with each iteration. Try this:

<?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; ?>

Upvotes: 0

Alok Patel
Alok Patel

Reputation: 8022

$counter is not increasing because you're not incrementing it in while loop. Increment in while loop to look something like this,

<?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; ?>

Upvotes: 0

Related Questions