I'm trying to get a counter to increment with and Advanced Custom Field repeater, but I'm also using a reset_rows()
to re-do the loop again to get more content from ACF. I can get one of them to work, but then when I try to do the second while
loop, it comes up with 0.
Here's what I'm doing so far:
<?php if (have_rows('projects')) : $project_counter = 0; ?>
<?php while (have_rows('projects')): the_row(); ?>
<div>
<a href="#project-<?php echo $project_counter; ?>" class="project-link">Link to project</a>
</div>
<?php project_counter++; endwhile;?>
<?php reset_rows();?>
<?php while (have_rows('projects')): the_row(); $project_counter = 0; ?>
<div id="project-<?php echo $project_counter; ?>">
<p>Project Content</p>
</div>
<?php $project_counter++; endwhile;?>
<?php endif;?>
I think that where I'm getting hung up is I'm not sure where the increment for the second after the reset_rows
should go since I know it's being defined in the previous if statement.
Overall, I'm trying to get it to render as:
<div>
<a href="#project-0" class="project-link">Link to project</a>
</div>
<div>
<a href="#project-1" class="project-link">Link to project</a>
</div>
<div>
<a href="#project-2" class="project-link">Link to project</a>
</div>
<div id="project-0">
<p>Project Content</p>
</div>
<div id="project-1">
<p>Project Content</p>
</div>
<div id="project-2">
<p>Project Content</p>
</div>
To reset the counter in the 2nd section put it before the while loop, like;
<?php $project_counter = 0; while (have_rows('projects')): the_row(); ?>
In your second loop you're reseting the $project_counter back to 0 at the start of each "while" so it's just getting incremented to 1 and set back to 0 each time if you remove it I think your code should run.
This
<?php while (have_rows('projects')): the_row(); $project_counter = 0; ?>
to this
<?php $project_counter = 0;
while (have_rows('projects')): the_row(); ?>