I would like to present an alternative solution as the one marked above didn’t work for me. This is how i did it, since i needed to keep the while loop and had to use get_sub_field();
Hope anyone can find it useful.
$flexible_row_number = 1;
$flexible_row_count = count( get_field("flexible_content_field_name") );
//Create random number out of number of rows.
$flexible_row_rand = rand(1, $flexible_row_count);
// check if the flexible content field has rows of data
if( have_rows('flexible_row_flexible') ):
// loop through the rows of data
while ( have_rows('flexible_row_flexible') ) : the_row();
//check if flexible row number equals random row number.
if ( $flexible_row_number == $flexible_row_rand ):
if( get_row_layout() == "layout"):
//If they do equal, add 1 to $flexible_row_number;
//If they dont equal, add 1 to $flexible_row_number;
Viewing 5 posts - 1 through 5 (of 5 total)
The topic ‘Display only one Flexible Content Field at random’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users