Support

Account

Home Forums General Issues Get Label in Repeater Field

Solved

Get Label in Repeater Field

  • I’m using a Repeater Field, and am trying to retrieve the label from one of the sub fields instead of the value.

    So for example this bit of code:

    <?php
     
    		$rows = get_field('blend');
    		if($rows)
    		{
    			echo '<ul>';
    		 
    			foreach($rows as $row)
    			{
    				echo '<li>' . $row['percentage'] . '% ' . $row['mix'] .'</li>';
    			}
    		 
    			echo '</ul>';
    		}
    
    		?>

    Is returning this:

    <ul>
    <li>30% blue</li>
    <li>70% light_pink</li>
    </ul>

    Which is almost what I’m trying to achieve, except I’m trying to return the Label instead of the Value. So “light_pink” is “Light Pink”.

    Any help with this is appreciated. Thanks in advance!

  • Hi @realph

    To load a select field’s label for the selected value, you will need to load the field object.

    Becuase this is a sub field, you need to use a function called get_sub_field_object. You can learn more about this function here:
    http://www.advancedcustomfields.com/resources/functions/get_sub_field_object/

    Please note that this function will not work with your current loop. You will need to change your loop to use a have_rows while loop like this:
    http://www.advancedcustomfields.com/resources/functions/have_rows/

    Thanks
    E

  • @elliot I still can’t wrap my head around how to do this. Do I put the get_sub_field_object inside the have_rows while loop like this:

    <?php if( have_rows('blend') ): ?>
     
    	<ul>
     
    	<?php
    		$select = get_sub_field_object('blend');
    		$value = the_sub_field('mix');
    		$myvariable = $select['choices'][ $value ];
    
    		while( have_rows('blend') ): the_row();
    	?>
     
    		<li>
    		    <p><?php echo $myvariable; ?></p>
    		</li>
     
    	<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>

    This is returning en empty unordered list. Not sure if this is a result of working late, or I’m just missing the concept completely.

  • Hi @realph

    Any sub field function such as ‘get_sub_field_object’ needs to be used WITHIN the loop, not outside the loop.

    Your code should be changed to this:

    
    <?php if( have_rows('blend') ): ?>
     
    	<ul>
     
    	<?php while( have_rows('blend') ): the_row(); 
    		
    		$select = get_sub_field_object('mix');
    		$value = get_sub_field('mix');
    		$myvariable = $select['choices'][ $value ];
    
    		?>
     
    		<li>
    		    <p><?php echo $myvariable; ?></p>
    		</li>
     
    	<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>
    

    I notices quite a few mistakes with your parameters such as ‘blend’ in the get_sub_field_object function. Blend is your repeater field, not your select field.

    Hopefully you can read over and understand the code I have posted.

    Thanks
    E

  • @elliot Of course, ‘mix’ can be called upon with get_sub_field_object, and is my select field! Thanks for jogging my tired brain.

  • Here’s a version to show both the label and value for a sub field in a repeater.

    <?php if( have_rows(‘distribution_list’) ): ?>
    <div id=”distributors”>

      <?php while( have_rows(‘distribution_list’) ): the_row();

      $select = get_sub_field_object(‘country’);
      $value = get_sub_field(‘country’);
      $label = $select[‘choices’][ $value ];

      ?>

      <li class=”<?php echo $value; ?>”><?php echo $label; ?>

      <?php endwhile; ?>

    </div>
    <?php endif; ?>

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

The topic ‘Get Label in Repeater Field’ is closed to new replies.