Home Forums General Issues Check if subfield in row is empty, and keep last item?


Check if subfield in row is empty, and keep last item?

  • Hello!

    I have a question regarding repeater fields.

    So I have a repeater with 2 subfields — Featured Image and Additional Image.

    Let’s say in the 1st row, I have both subfields filled in. But in the 2nd row, only Additional Image is filled in.

    Screenshot for reference:

    By leaving the Featured Image subfield blank in the 2nd row, I lose it completely in the front-end. I’m looking to be able to check if a subfield is empty, and if it is, then keep the last item.

    Currently my php is set up like this:

    <?php if(have_rows(‘large_image_layout’)) { ?>
       <?php while(have_rows(‘large_image_layout’)) {
       $featured_image = get_sub_field(‘featured_image’);
     <?php } ?>
     <?php } ?>
     <div class=”featured-image” style=”background-image:url(<?php echo $featured_image; ?>);”> </div>

    I hope that makes sense. Looking forward to your thoughts and responses!

    Thanks so much!

  • This is an example or how to keep previous row values if the current row values are empty.

    if (have_rows('repeater')) {
      $field_1 = false;
      $field_2 = false;
      while (have_rows('repeater')) {
        if (get_sub_field('field_1')) {
          $field_1 = get_sub_field('field_1');
        if (get_sub_field('field_1')) {
          $field_1 = get_sub_field('field_1');
        // use $field_1 and $field_2
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.