Home Forums ACF PRO Get Value of Sub-Field Outside of While Loop


Get Value of Sub-Field Outside of While Loop

  • Hi there,

    I have been working on this all night but I cannot get the code to output how I need it to.

    Basically, I have a nested repeater field and what I am trying to do is echo the name of the a sub-field in the second repeater before the second repeater while loop is run.

    Hopefully someone can help me out because I am ready to give up and I really need this to work. Have mercy on a very tired gal! Code below.


    // check for rows (parent repeater)
    if( have_rows(‘affiliations_&_links’, ‘option’) ): ?>
    <div id=”to-do-lists”>

    // loop through rows (parent repeater)
    while( have_rows(‘affiliations_&_links’, ‘option’) ): the_row(); ?>

    // check for rows (sub repeater)
    if( have_rows(‘affiliations_repeater’, ‘option’) ): ?>
    <h3><?php the_sub_field(‘link_type’, ‘option’); ?></h3> // this is where I am having issues. This sub_field is a part of the second repeater but if I put it after the second while loop, it repeats every time


    // loop through rows (sub repeater)
    while( have_rows(‘affiliations_repeater’, ‘option’) ): the_row();

    // display each item as a list
    <li><?php the_sub_field(‘al_name’, ‘option’); ?></li>
    <?php endwhile; ?>
    <?php endif; ?>

    <?php endwhile; ?>
    <?php endif; ?>

  • I was able to figure it out. Never was able to get to the sub_field outside of the second nester so I put in a counter. In hindsight, it seems incredibly glaringly obvious but oh, well! Pasting for anyone else who might have the need. It might not be the smartest way to go about it but this did the trick for me:

        if( have_rows('affiliations_&_links', 'option') ):
          while( have_rows('affiliations_&_links', 'option') ): the_row(); 
        	if( have_rows('affiliations_repeater', 'option') ):
    				$x = 1;
    				echo '<ul>';	
    				while( have_rows('affiliations_repeater', 'option') ): the_row();
    						echo '<li>';
    						 // Flag for the first row
    						if ($x==1) :
              					echo '<h5>';
    						echo the_sub_field('link_type', 'option');
    						echo '</h5>';
           					else :  endif;
    						echo the_sub_field('al_name', 'option');
    						echo '</li>';
          			$x++; endwhile;  // ends affiliation repeater
    						echo '</ul>';
    		endif; // ends affiliation repeater
    	endwhile; // ends affiliation and links
    	endif;// ends affiliation and links
  • When calling for values of subfields, you shouldn’t specify the $post_id argument as in the_field() function call, e.g. your 'option' here: the_sub_field('link_type', 'option'). You should just leave it like that: the_sub_field('link_type'). This might be the cause of your problem, although I am not really sure.

    I suggest you to take a closer look on how nested loops are handled in the_sub_field() function documentation in the “Examples” section.

  • You do not need the post ID for sub fields.

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

The topic ‘Get Value of Sub-Field Outside of While Loop’ is closed to new replies.