Support

Account

Home Forums General Issues Get select field label within repeater

Solved

Get select field label within repeater

  • I want to get the value of ‘select’ field within my repeater field. The solutions I’ve found don’t seem to work – I think it’s because I’m within an array. Here’s my code – I want the H2 in this example to start with the label of the ‘area’ select field, instead of the value:

    $representativeArray = get_field('sales_representatives');
    if (!empty($representativeArray)) {
    	usort($representativeArray, 'sortByGroup');
    	$last_group = ''; // initialize the group name
    	foreach ($representativeArray as $representative) {
    		$this_group = $representative['area'];
    		if ($this_group != $last_group) {
    
    			if ($last_group != '') { ?>
    				
    				</div>
    			</div>
    		</div><!-- ./remodal -->
    				
    			<?php }
    
    		$last_group = $this_group; ?>
    
    		<div class="remodal sales-representatives-remodal" data-remodal-id="modal-<?php echo $representative['area']; ?>">
    			<button data-remodal-action="close" class="remodal-close"></button>
    					
    			<h2><?php echo $representative['area']; ?> Sales Team</h2>
    
    			<div class="row">
    				<div class="col-sm-8 text-left">					
    						
    					<?php }
    
    				if( $representative['name'] ) {
    					$representative_name = '<i class="fa fa-user-circle color-'.$representative['area'].'"></i><strong>'.$representative['name'].'</strong><br>';
    				}
    				if( $representative['email'] ) {
    					$representative_email = '<i class="fa fa-envelope"></i><a href="mailto:'.$representative['email'].'">'.$representative['email'].'</a><br>';
    				}
    				echo $representative_name;
    				echo $representative_email;
    				echo '
    ';
    
    	} // end foreach
    } // end if personArray
    ?>

    for reference I have tried this solution, but it’s not working for me:

    Any help appreciated!

  • Hi @elsmore

    For nested fields such as your select field, you will need to make use of a repeater loop. This can be achieved by using the have_rows() loop and then call the select field within this by making use of the get_sub_field() function as described on this page: https://www.advancedcustomfields.com/resources/repeater/

    I hope this info helps.

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

The topic ‘Get select field label within repeater’ is closed to new replies.