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

时间:2016-11-02 10:20:44

标签: php wordpress advanced-custom-fields

我正在尝试为手风琴添加一个简单的计数器变量给我的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; ?>

3 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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