Support

Account

Home Forums General Issues Question about new Group field

Helping

Question about new Group field

  • In the group field docs the 2nd example code shows how a group field would be called using the have_rows function.

    I am a little unclear how I would to duplicate groups on the fly and use the have_rows function.

    My example is I have x4 ‘highlights’ groups. Each group has the same content fields: title, desc, link, image.

    I was going to treat each of the 4 groups individually and name them ‘1st highlight group’, ‘2nd highlight group’ etc… But after looking at the example code I’m not sure if that is the correct way to approach this?

    How would I create one single group called ‘highlight group’ and then duplicate this on the relevant page and use the have_rows function? Or is this not the intention?

    Thanks!

  • Hi @designlobby

    Groups just have one row.. but you can use a Repeater of Groups.

    I did this for a test:

    Repeater: highlights
    —> Group: highlight
    ——> Text: title
    ——> Text: desc
    ——> Link: link
    ——> Image: image

    Code example:

    <?php
    if ( have_rows('highlights') ) {
      while ( have_rows('highlights') ) { // Repeater Loop
        the_row();
        if ( have_rows('highlight') ) {
          while ( have_rows('highlights') ) { // Group Loop
            the_row();
            $title = get_sub_field('title');
            $desc = get_sub_field('desc');
            $link = get_sub_field('link');
            $image = get_sub_field('image');
            echo '<h2>' . $title . '</h2>';
    ?>
    <a href="<?php echo $link['url']; ?>"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></a>
    <p><?php echo $desc; ?></p>
    <?php
          }
        }
      }
    }
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.