Support

Account

Home Forums General Issues Setting variables with repeater and image array

Solved

Setting variables with repeater and image array

  • I have a repeater and one of the fields is an image array. I’m using array since a script needs me to pull in the image height.

    I want to set a few variables with a while statement so I can use them in a foreach to spit them out for a slider, then use them again for a different script later down the page.

    The code below just loops over the first image in the slider over and over again.

    I’m a PHP novice. What am I doing wrong?

    gallery_repeater is the name of the repeater
    the image subfield is gallery_repeater_image.
    There are also a couple other text-based fields that aren’t part of the code.

    <?php 
    if( have_rows('gallery_repeater') ):
      
       // Loop through rows.
       while( have_rows('gallery_repeater') ) : the_row();
       $repeats = get_field('gallery_repeater');
       $repeater_image = get_sub_field('gallery_repeater_image');
       $default_title= $repeater_image['title'];
    
    endwhile;
    foreach( $repeats as $repeat ) {
    
    var_dump ($default_title);
    }
    // No value.
    else :
      // Do something...
    endif;
    
    ?>
    
  • The problem is that you are using get_field('repeater') inside the loop for the repeater while(have_rows(repeater')) which is confusing the while loop. If you want to get the. If you want to get the entire repeater as an array then you need to do this outside of the while loop.

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

You must be logged in to reply to this topic.