Home › Forums › Add-ons › Repeater Field › Select random custom field from nested repeater › Reply To: Select random custom field from nested repeater
So, I ended up doing this:
<?php if( have_rows('section_content') ):
$rows = array();
$i = 0;
while ( have_rows('section_content') ):
the_row();
$rows[$i] = get_sub_field('content_option');
$i++;
endwhile;
$i = mt_rand(0,count($rows)-1);
echo $rows[$i];
else:
// some other stuff
endif; ?>
Hackfest 2015! But at least I’m getting the results I need. I’m guessing this isn’t very efficient, but I’m going to end up putting caching on this, and we’re talking about a very small audience (maybe 1000 visitors per month across 6 sites). So I think it will end up working out fine.
Still, I would love it if there’s a more native way to do what I’m doing, where I just pull one result, instead of pulling all of them and then selecting one from an array. It feels dirty…
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.