Support

Account

Home Forums Add-ons Repeater Field Random repeater field and limit output

Solved

Random repeater field and limit output

  • Hi!

    I want to be able to put in how many rows as possible into a repeater field, but I just want to display 5 at the time and randomize these 5 fields every time the page load.

    So what I’m looking for is to random repeater field, but limit the output to maximum 5.

    I have tried to shuffle between answers from how to random and how to limit but can’t get it to work.

    So now I have a code that works, that output maximum 5 rows, even though I have 20 fields.

    So how can I random this fields now, but limit to 5?

    My code right now is: `<? if( get_field(‘referenser’) ): $i = 0; ?>

    <?php while( has_sub_field(‘referenser’) ): $i++;

    if( $i > 5 )
    {
    break;
    }

    ?>

    <?php $bilder = wp_get_attachment_image_src(get_sub_field(‘referensens_logotype’), ‘full’); ?>

    <div class=”referens”><img src=”<?php echo $bilder[0]; ?>”></div>

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

    • Elliot

    • October 30, 2013 at 4:50 pm

    Hi @Henric Åkesson

    To do this, you can’t use the has_sub_field function. Instead, you need to use the more basic PHP foreach loop through the repeater field data.

    This way, you can use shuffle to randomize the repeater rows, and then loop through them using a counter to break the loop at row 5.

    Shuffle: http://support.advancedcustomfields.com/forums/topic/random-repeater/
    limit: http://support.advancedcustomfields.com/forums/topic/limiting-results-from-repeater-field/

    Good luck

    Thanks
    E

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.