Home › Forums › Add-ons › Repeater Field › Using a repeater field in a for loop
Hi,
I would like to use a repeater field in a for loop but I’m having trouble getting the value.
I’ve created a dropdown field with the options of 1,2,3,4,5 and this field is called star_rating.
I have the following function to loop out stars dependent on what has been selected from the star_rating field.
(Please note that this isn’t the full function, just the part that isn’t working.)
$rating = the_sub_field('star_rating');
for ($i = 0; $i < $rating; $i++) {
echo 'star';
}
Am I missing something simple or isn’t this possible? Can you help please?
If you’re select field only allows a single value then the code should be right. If the select field allows multiple selections then it will return an array of selected values.
If you originally created the field and allowed multiple selections… then edited a post and made a selection, then you changed the field to only allow a single value then it is probably still returning an array for this value.
Although this is not the only thing that could be wrong, but base on the code and information supplied that’s the best I can offer.
My select field doesn’t allow multiple options.
This is my full code if you can spot anything amiss?
<?php if( have_rows('customer_review') ):
$count = 0
?>
<div class="product-reviews">
<?php while( have_rows('customer_review') ): the_row(); ?>
<div class="one-third<?php if ($count == 0) {echo ' first';} ?>" id="review-container">
<p><?php the_sub_field('review'); ?></p>
<p><?php the_sub_field('customer_name'); ?></p>
<p><?php the_sub_field('product'); ?></p>
<?php $rating = the_sub_field('star_rating'); for ($i = 0; $i < $rating; $i++) { echo '<p>something</p>'; } ?>
<?php if ($count == 2) { $count = 0;} else { $count++; } ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
Actually, no, I don’t. Try outputting the value of the field to see what it is
<?php $rating = the_sub_field('star_rating');
echo '<p>RATING = '; var_dump($rating); echo '</p>';
Hi John,
Problem solved! I needed to change this line:
$rating = the_sub_field('star_rating');
To this:
$rating = get_sub_field('star_rating');
Thank you very much for your help regardless!
Sorry, I missed that completely, and I looked at it twice and copied it once. My skill at spotting small errors is lacking lately. Glad you got it figured out.
The topic ‘Using a repeater field in a for loop’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.