Home Forums Add-ons Repeater Field Using a repeater field in a for loop


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++; } ?>
    <?php endwhile; ?>
    <?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>';
  • Thanks for getting back to me John. I did that and it returns NULL

  • 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.

  • Haha no problem, it happens to us all! 🙂

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

The topic ‘Using a repeater field in a for loop’ is closed to new replies.