Support

Account

Home Forums Add-ons Repeater Field reset_rows() not working

Solved

reset_rows() not working

  • I have an expandable tab section, which is set up as a Repeater field, within a Flexible Content layout. The concept is like an expandable FAQ. I need to loop through the “answers” once in the same column (for mobile) and then again in a second column (for desktop). I have tried to use reset_rows(); to run the loop a second time, but it’s not working for me.

    Can anyone help spot what’s wrong here?

    <?php if( have_rows('program') ):
    									$programcount = 0; ?>
    									<div class="col-xs-12 col-lg-6 nopadding">
    										<?php while( have_rows('program') ):
    											$programcount++;
    											the_row(); ?> 
    											<div class="program">
    												<a class="question collapsed accordion-button" data-bs-toggle="collapse" href="#answer<?php echo $programcount; ?>" role="button" aria-expanded="false" data-bs-target="#answer<?php echo $programcount; ?>" aria-controls="answer<?php echo $programcount; ?>">
    													<div class="text">
    														<?php the_sub_field('title'); ?>
    													</div>
    													<div class="arrow-container"><i class="fas fa-caret-down"></i></div>
    												</a>
    												<div class="answer mobile accordion-collapse collapse" id="answer<?php echo $programcount; ?>" data-bs-parent="#tab-section<?php echo $tab_section_count; ?>">
    													<div class="card card-body">
    														<?php the_sub_field('description'); ?>
    													</div>
    												</div>
    											</div>
    										<?php endwhile; ?>
    									</div>
    								<?php endif;
    								reset_rows(); 
    								if( have_rows('program') ): 
    									$programcount = 0; ?>
    									<div class="col-lg-6 nopadding desktop">
    										<?php while( have_rows('program') ):
    											$programcount++;
    											the_row(); ?>
    											<div class="program">
    												<div class="answer mobile accordion-collapse collapse" id="answer<?php echo $programcount; ?>" data-bs-parent="#tab-section<?php echo $tab_section_count; ?>">
    													<div class="card card-body">
    														<?php the_sub_field('description'); ?>
    													</div>
    												</div>
    											</div>
    										<?php endwhile; ?>
    									</div>
    								<?php endif; ?>
  • If I move reset_rows() just inside the second loop, it appears to work.

  • reset_rows() only needs to be used if you need to reset to the beginning in the middle before before have_rows() returns false. I’m not sure when this was changed. If you call it after the loop then you are likely resting something else, for example if this is an nested repeater.

  • Thanks for chiming in. Are you saying that I shouldn’t use it at all in this scenario? Because it doesn’t seem to work without it.

  • Yes, that’s what I’m saying. It is not necessary once the have_rows() loop is completed. It is only needed if you exit the first loop prematurely.

  • You’re right, of course. There must have been some other variable amiss when I originally tested. All is well now. Thank you again for your help.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.