0

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; ?>
2
  • What is the error you're getting? Commented Nov 2, 2016 at 10:23
  • The input id's are all the same so the counter loop doesn't seem to be ascending using <?php echo $counter;?> Commented Nov 2, 2016 at 10:25

3 Answers 3

5

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; ?>
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.