Support

Account

Home Forums Feature Requests get_sub_fields()

Solving

get_sub_fields()

  • Hi,

    Sometimes inside the repeater there is a need to add some logic (for example to display fields conditionally). In order to take out the logic outside the template it would be nice to have get_sub_fields() function that returns an array of all the sub fields.

    So simply it’s a request for a function that works like get_fields() but inside a repeater loop.

    Thanks.

    Example:
    At the moment the code looks like this:

    
    <ul>
        <?php while (have_rows('menu', 'options')): the_row(); ?>
            <li>
                <?php
                $link = get_sub_field('external_link');
                if (!get_sub_field('has_extarnal_link')) {
                    $link = get_permalink(get_sub_field('page')->ID);
                }
                ?>
    
                <a href="<?php echo $link ?>">
                    <?php the_sub_field('label'); ?>
                </a>
            </li>
        <?php endwhile; ?>
    </ul>
    

    If there was get_sub_fields() function it could look like this:

    
    <?php // Template ?>
    <ul>
        <?php while (have_rows('menu', 'options')): the_row(); ?>
            <li>
                <a href="<?php echo get_menu_item_url(get_sub_fields()) ?>">
                    <?php the_sub_field('label'); ?>
                </a>
            </li>
        <?php endwhile; ?>
    </ul>
    
    
    <?php
    // functions.php
    
    function get_menu_item_url($sub_fields)
    {
        $link = $sub_fields['external_link'];
        if (!$sub_fields['has_extarnal_link']) {
            $link = get_permalink(get_sub_field('page')->ID);
        }
        return $link;
    }
    
    ?>
    
  • I’ll mark this request for the developer to take a look at when he has time.

  • Hi, any changes? It seems like it’s forgotten.
    This feature will also help me a lot. How can I help with implementation?

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

The topic ‘get_sub_fields()’ is closed to new replies.