Support

Account

Home Forums ACF PRO have_rows causing infinite loop when getting repeater values from another page Reply To: have_rows causing infinite loop when getting repeater values from another page

  • I’ve done what i can do, and I also let it peculate in my head for a while.

    The reason that I wasn’t seeing how to make this recursive was that I was over complicating it, trying to get your initial loop into the recursion and do the recursion at the same places that you were loading alternate files and also including a check for the target section, but all that really wasn’t needed. Instead the only place it really needs to recurse is when doing a nested block reference.

    I haven’t tested this out 100%, but it looks like it should work.

    Yeah, the whiles and ifs are nested a bit deep, but I couldn’t see a way to avoid that.

    
    <?php 
      
      // start the blocks content
      // this is the initial loop
      if (have_rows('blocks')) {
        while (have_rows('blocks')) {
          the_row();
          $layout = get_row_layout();
          if ($layout == 'block_reference') {
            $target_page = get_sub_field('target_page');
            $target_section = get_sub_field('target_block_id');
            flexible_block_reference($target_page, $target_section);
          } else {
            // flexible_repeater_block
            if (have_rows('repeater_items')) {
              while (have_rows('repeater_items')) {
                the_row();
                echo 'Item: ' . get_sub_field('title') . '<br />';
              } // end while
            } // end if
          } // end if/else
        } // end while
      } // end if
      
      function flexible_block_reference($post_id, $section) {
        // recurstive function 
        if (have_rows('blocks', $post_id)) {
          while (have_rows('blocks', $post_id)) {
            the_row();
            if (get_sub_field('block_id') == $section) {
              $layout = get_row_layout();
              if ($layout == 'block_reference') {
                $target_page = get_sub_field('target_page');
                $target_section = get_sub_field('target_block_id');
                // recurse
                flexible_block_reference($target_page, $target_section);
              } else {
                // flexible_repeater_block
                if (have_rows('repeater_items')) {
                  while (have_rows('repeater_items')) {
                    the_row();
                    echo 'Item: ' . get_sub_field('title') . '<br />';
                  } // end while
                } // end if
              } // end if/else
            } // end while
          } // end if target section
        } // end if
      } // end function
      
      
    ?>