Support

Account

Home Forums Add-ons Repeater Field Nested Foreach on ACF Repeater

Solving

Nested Foreach on ACF Repeater

  • Hi there,

    I have been scouring the web trying to get this to work but to no avail. I have read (and tested) all available code for getting a nested ACF repeater to show via for each. Unfortunately, I have to use a foreach (at least my understanding is that I have to) because I need the first repeater to be sorted by name. Here is an outline of my ACF set up:

      A. Class List (Repeater)
      a1. Speaker (Text – this is what it is sorted by)
      a2. Class Group (Repeater)
      ai. Class Title

    I am pretty sure that the issue is that I do not know how to connect the sub repeater field to the “main” repeater field. I also tried combining a while loop inside and it got messed up by the foreach statement.

    My code thus far:

    <?php
    /**
     * The template used for displaying page content in page-classes.php
    
     */
    
    ?>
    <div class='content-wrap'>
    		
    		<h1 class="entry-title"><?php the_title(); ?></h1>
    <?php 
    // Get repeater value
    $repeater = get_field('class_list');
    // Obtain list of columns
    foreach ($repeater as $key => $row) {
    	$speaker[$key] = $row['cl_speaker'];
    }
    
    // Sort the data, ascending
    array_multisort($speaker, SORT_ASC, $repeater);
    
    // Display newly orded columns
    echo '<ul class="br-sl">';
    foreach( $repeater as $row ) { ?>
    	<a name="top">&nbsp;</a>
    		<li>
    			<a href="#<?php echo $row['cl_speaker']; ?>"><?php echo $row['cl_speaker']; ?></a>
    		</li>
    				
    	<?php } 
    	echo '</ul>';
    foreach( $repeater as $row ) { ?>
    <div>
    	<?php echo $row['cl_speaker']; ?><a name="<?php echo $row['cl_speaker']; ?>">&nbsp;</a>
    
    	<?php // a nested foreach - under Speaker, I need each class they'll teach listed
    $cgroup = get_field('class_group');
    // Obtain list of columns
    foreach ($cgroup as $key => $row) { // I have tried changing both the key and row variables with no change in results
    	$ctitle[$key] = $row['cl_title'];
    }
    	foreach( $cgroup as $row ) 
    		echo $row['cl_title'];
    	?>
    	
    	</div>
    	<?php } ?>
    	
    </div><!-- .content-wrap -->
  • I have done this and it worked really well. It was several years ago so the syntax has probably changed and it is a large file. So let me know if you want me to share it.

  • Please! I would so appreciate seeing it. I haven’t been able to solve the issue yet so I am completely open. Thank you so much!

  • OK I tried to narrow the code down to the section with the repeaters. Short explanation: Agenda day is a repeater. Inside that is the agenda schedule, and nested inside that is the agenda list of events (repeater) and each event had speakers (repeater). And inside that each speaker has their own fields. At the time this was an extremely ambitious project for me and do not think I would undertake it again. Note that there were other ACF fields on the page to build a slideshows, sponsor scrollers and other info all built with ACF. The calls for each repeater are basic and the trick was to call the repeater fields correctlly so that the sub_fields, including the nested repeaters could then be displayed. Again, I am not a master coder and do not claim this was completely correct at the time. I think the only way I could do this was because I didn’t know better LOL.

      <div id="fullagenda">                                            
    <?php if( have_rows('daily_agenda')): ?>
    	<ul>	
     	<?php  while ( have_rows('daily_agenda') ) : the_row();  ?>
        	<li>
                <div class="bluetitle" style ="background: <?php the_field('event_color'); ?>;">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-12">
                                                                        
                                <h3><?php the_sub_field('agenda_day'); ?></h3>
                            </div><!-- col-md-12 -->                                          
                        </div><!-- .row -->                                    
                    </div><!-- .container -->
                </div><!-- #bluetitle -->                                                 
                <div class="fullagenda">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-12">
                            
                               <?php if( have_rows('agenda_schedule')): ?>                                    
                                <ul class="agendaul">  
                                                    
                                        <?php while ( have_rows('agenda_schedule') ) : the_row();  ?>
                
                                            <li class="agendali">
                                            	
                                                <div class="timeplace">
                                            
                                                        <div class="timeline">
                                                        
                                                        <p><?php the_sub_field('time_range');?></p>
                                                        
                                                        </div><!-- .timeline -->
                                                        
                                                        <div class="venue">
                                                        
                                                        <p><?php the_sub_field('venue');?></p>
                                                        
                                                        </div><!-- .venue -->
                                                        
                                                 </div><!--timeplace -->
                                                <div class="programbox">
                                                    <h4><?php the_sub_field('program_title'); ?></h4>
                                                     <div class="pdesc"><?php the_sub_field('program_description');?></div>
                                                       
                                                </div><!-- .biobox -->
                                                
                                                <div class="minispeakers">                                            
                                                <?php if( have_rows('program_speakers') ): ?>      
                                                    <ul>
                                                    <?php while ( have_rows('program_speakers') ) : the_row();  ?>
                                                    
                                                        <li class="mini">
                                                            <div class="minipic">
                                                            <img src="<?php the_sub_field('mini_portrait');?>" />
                                                            </div><!-- .companypic -->
                                                            <div class="mininfo">
                                                                <span class="minname"><?php the_sub_field('mini_company_name'); ?></span><br />
                                                                <span class="mincomp"><?php the_sub_field('mini_speaker_name');  the_sub_field('speaker_time'); ?></span>
                                                            </div><!-- .mininfo -->
                                                        </li>
                                                        
                                                    <?php endwhile; ?>  
                                                    </ul>
                                                <?php endif; ?>
                                                </div>
                                                
                                            </li>
             
                                        <?php endwhile; ?>
             
                                </ul>                            
                                <?php endif; ?>  
                                    
                               </div><!-- col-md-12 -->                                     
                        </div><!-- .row -->                                    
                    </div><!-- .container -->
                </div><!-- .fullagenda -->
                </li>                             
         <?php endwhile; ?>
         </ul>                
    <?php endif; ?>
       </div><!-- #fullagenda --> 
  • Thank you so much for sending that over. I’ll explore it more tomorrow but wow, you were ambitious! One thing that I don’t see is a sorting of the repeater – which is something I need to do. I need the first repeater to be sorted by name (ABC) and then don’t really care about the second (I’d love to have that sorted as well but I’ll take what I can get!). From my research, in order to do the sorting, I need to use multisort, which requires the foreach. It’s about 3am my time so my brain might not be fully engaged at the moment. Can’t wait to take another look tomorrow!

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

The topic ‘Nested Foreach on ACF Repeater’ is closed to new replies.