Support

Account

Home Forums Feature Requests get_sub_fields()

Solving

get_sub_fields()

    • wube

    • October 9, 2015 at 2:13 am

    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 4 posts - 1 through 4 (of 4 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.