Support

Account

Home Forums Add-ons Repeater Field Call LABELS of selection field inside repeater

Solved

Call LABELS of selection field inside repeater

    • juiceex

    • November 19, 2019 at 7:14 am

    Here’s what I am currently seeing as a result of my code:

    Current Result

    <?php
    if( get_field('degrees') ) {
        echo '<strong>Degrees: </strong>'; 
        while ( have_rows('degrees') ) : the_row();
         $array[] = get_sub_field('degree_select'); 
        endwhile;
        $foo = implode(', ', $array);
    	
        echo $foo;
    }
    ?>

    The sub_field “degree_select” is a selection field that has Labels and Values. I only want to call the Labels in a comma separated list.

    Typically I would write something like:

    <?php while (have_rows('degrees') ): the_row();
    $value = get_sub_field('degree_select');
    echo $value['label'];
    endwhile; ?>

    .. which works fine, but I haven’t been able to get it working using implode for this comma separated list yet. Any help would be greatly appreciated.

    Thank you.

    • juiceex

    • November 20, 2019 at 12:24 am

    I figured out the solution. I was to use array_column which helped me focus on that single column of information.

    <?php if( get_field('degrees') ) {
       while ( have_rows('degrees') ) : the_row();
          $array[] = get_sub_field('degree_select'); 
       endwhile;
       $foo = implode(', ', array_column($array, 'label'));
       echo $foo;
       }								
    ?>

    You can learn more about those here:
    https://www.php.net/manual/en/function.array-column.php

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.