Support

Account

Home Forums Add-ons Repeater Field Repeater within a repeater options page – grab data from first repeater loop

Solved

Repeater within a repeater options page – grab data from first repeater loop

  • Hi, this might be a bit complicated to explain so bare with me (couldn’t even summarise it in the topic.) the image and code below should summarise

    image

    Essentially I want the document link to display on the post object pages within the sub-repeater field. I’ve managed to get the code working without a sub-repeater (post object only) but that means the document can only be displayed on one page… which isn’t ideal as the client wants the documents accessible on multiple pages – as well as being more user friendly in regards to organising documents (they add and remove documents frequently)

    <?php if ( have_rows('documents2', 'option') ) :  
      $currentPageID = $post->ID;
      $countCheck = 0;
    
      while ( have_rows('documents2', 'option') ) : the_row(); 
    
        if ( have_rows('document_pages') ) : 
    
          while ( have_rows('document_pages') ) : the_row(); 
    
            $post_object = get_sub_field('document_page');
    
            if ($post_object->ID == $currentPageID) {
              $countCheck++;
            }
        
          endwhile; 
    
        endif; 
    
      endwhile;

    This is working properly – it’s counting both instances of homepage.

      if($countCheck > 0): 
      ?>
    
          <div class="documents">
            <div class="container">
              <div class="row">
                <div class="col-xs-12 col-md-8 col-md-offset-2">
                  <h2 class="text-center">Documents</h2>
                  <?php 
                  $documentCategories = array();
    
                  while ( have_rows('documents2', 'option') ) : the_row();
          
                    if ( have_rows('document_pages') ) :  
    
                      while ( have_rows('document_pages') ) : the_row(); 
                      $post_object = get_sub_field('document_page');
    
                        if ($post_object->ID == $currentPageID) {

    This is where the code begins to falter – I want to go back to the parent repeater and display the respective category on the page – but as it’s a parent loop within a parent loop it loops infinitely (i believe… or at least the page times out with a 501 error!)

      if ( have_rows('documents2', 'option') ) :  
    
                            while ( have_rows('documents2', 'option') ) : the_row(); 
                            $terms = get_sub_field('document_category2');
                  
                              if( $terms ): 
                                
                                foreach( $terms as $term ):
                                $tag = $term->name;
                                
                        
                                  echo $tag;
                                  
                                endforeach;
                            
                              endif;
    
                            endwhile;
                        
                          endif;
    
                        } 
    
                      endwhile;
    
                    endif;
    
                  endwhile;
    
                  ?>
                </div>
              </div>
            </div>
          </div>
    
      <?php endif; ?>
    
    <?php endif; ?>
    

    I’ve tried a break and return but it means losing the $post_object data… any help would be much appreciated 🙂

  • Hi @tdoc42

    You are not supposed to loop trough the parent repeater again in your code. You should be able to get the ‘document_category2’ subfield after the child repeater loop. It should be something like this:

    while ( have_rows('documents2', 'option') ) : the_row();
          
        if ( have_rows('document_pages') ) :  
    
            while ( have_rows('document_pages') ) : the_row(); 
                $post_object = get_sub_field('document_page');
    
                if ($post_object->ID == $currentPageID) {
                    // do something here
                }
            endwhile;
            
        endif;
            
        // you can access the subfields of "documents2" again here
        $terms = get_sub_field('document_category2');
    
    endwhile;

    This page should give you more idea about it: https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/.

    I hope this helps 🙂

  • Sorry – I may not have made myself clear – I want to grab the terms/categories from within the if ($post_object->ID == $currentPageID) so only the documents connected to the post object display on the respective page.

    The method you have sent me grabs ALL of the terms/categories and displays it on all pages…

    Just to reiterate, the client wants the document uploader to be accessible through one page (options) rather than on a page by page basis, as it is too difficult for them to organise.

  • Hi @tdoc42

    If you want to use it inside of the child repeater loop, then you need to put the terms in a variable first. It should be something like this:

    while ( have_rows('documents2', 'option') ) : the_row();
          
        if ( have_rows('document_pages') ) :  
        
        // get the terms and put it in a variable for later use
        $terms = get_sub_field('document_category2');
    
            while ( have_rows('document_pages') ) : the_row(); 
                $post_object = get_sub_field('document_page');
    
                if ($post_object->ID == $currentPageID) {
                    // you can use the $terms variable here
                    print_r($terms);
                }
            endwhile;
            
        endif;
    
    endwhile;

    I hope this makes sense 🙂

  • thanks, putting $terms before the if (have_rows(‘document_pages’) ): does the trick, should have known! thank you 🙂

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

The topic ‘Repeater within a repeater options page – grab data from first repeater loop’ is closed to new replies.