Support

Account

Home Forums Add-ons Repeater Field How to group sub fields?

Helping

How to group sub fields?

  • Hello. I am hoping someone can help me with how to display sub fields into groups. I have the following code but am sure I am missing something fundamental. Any help appreciated.

    <?php while (have_posts()) : the_post(); ?>

    <?php if( have_rows(‘repeater’) ): ?>
    <?php while( have_rows(‘repeater’) ): the_row(); ?>

    <?php

    $name = get_sub_field(‘name’); // retrieves; John Joan Ravi Devi Olaf Freya
    $fruit = get_sub_field(‘fruit’); // retrieves: apples oranges apples mangoes oranges apples

    $arr_fruit[] = $fruit; // array
    $arr_unq_fruit = array_unique($arr_fruit); // apples oranges mangoes

    $fave_fruit = array();
    foreach ($arr_unq_fruit as $key => $val) {
    $fave_fruit[] = $val;
    }
    ?>

    <?php

    $n=0;
    while ( $n< count($arr_unq_fruit) ) {

    if( $fruit == $fave_fruit[$x] ) {
    echo ” . $name . ‘ likes ‘ . $fave_fruit[$x] . ‘. <br/>’ ;
    }

    $n++;
    }
    ?>

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

    <?php endwhile; ?>

    The above matches names with fruits but how to get and display in fruit groups/blocks?

    Above result:

    John likes apples
    Joan likes oranges
    Ravi likes apples
    Devi likes mangoes
    Olaf likes oranges
    Freya likes apples

    Desired result:

    John likes apples
    Ravi likes apples
    Freya likes apples

    Joan likes oranges
    Olaf likes oranges

    Devi likes mangoes

  • OK so it’s probably more a regular coding issue than an ACF one but would still be grateful for any suggestions on best way to approach working with rows of sub fields which can be handled in groups (for formatting etc). Multidimensional array maybe?

    $name = get_sub_field(‘name’); // John Joan Ravi Devi Olaf Freya
    $fruit = get_sub_field(‘fruit’); // apples oranges apples mangoes oranges apples
    $when = get_sub_field(‘when’); // breakfast breakfast lunch dinner breakfast lunch

    To get this (could be grouped either by fruit or time):

    John likes apples at breakfast
    Ravi likes apples at lunch
    Freya likes apples at lunch

    Joan likes oranges at breakfast
    Olaf likes oranges at breakfast

    Devi likes mangoes at dinner

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

The topic ‘How to group sub fields?’ is closed to new replies.