How to ACF list label not value

Im using ACF repeater to generate results form a list, which is all working fine. But, rather than display the list value, I want to display the list label.

Here is the code:

Read More
        <ul class="notifications-content">
            <?php while(has_sub_field('notification')):
                $house = get_sub_field('house');    
                $year  = get_sub_field('year');
                $date = DateTime::createFromFormat('Ymd', get_sub_field('date'));
                $newDate = date("D j F", strtotime($date->format('d-m-Y')));
                ?>
                <li class="<?php echo safe_url($house); ?> <?php echo $year; ?>">
                    <h2 class="black"><?php echo str_replace($replace, '', get_sub_field('message')); ?></h2>
                    <h3 class="interstatebold white uppercase"><?php echo $house; echo $year ? ", $year"  : ''; echo ' | '.$newDate; ?></h3>
                    <?php echo get_sub_field('urgent') ? '<div class="circle"></div>' : ''; ?>
                </li>
            <?php endwhile; ?>
        </ul>

How do I get the label?

Related posts

Leave a Reply

3 comments

  1. Take a look at this official documentation about get_field_object

    There is a usage example in the bottom:

    Get a field object and display it with it’s value

    $field_name = "text_field";
    $field = get_field_object($field_name);
    
    echo $field['label'] . ': ' . $field['value'];
    

    You can use this in your repeater to grab that field_object you want and display the label.

  2. Instead of using has sub field, use get_field(‘platforms’). Put respective repeater field name in place of ‘platforms.’ Platforms is the name of repeater field.

    By using foreach loop with key=>value pair, you get key as well as value.
    Try using following code.

    $platforms = get_field('platforms');
    foreach( $platforms as $val ){
                            foreach($val as $key=>$v){
                                echo $key.'=>'.$v.'<br>';
                            }
                        }
    
  3. You can use it the same way you would call the field values by passing it through these functions (add to functions.php):


    function get_field_label($slug) {
        $field = get_field_object($slug); 
        return $field['label'];
    }
    

    Usage:

    <?php echo get_field_label('field-slug'); ?>


    function the_field_label($slug) {
        $field = get_field_object($slug); 
        echo $field['label'];
    }
    

    Usage:

    <?php the_field_label('field-slug'); ?>