Home › Forums › Add-ons › Flexible Content Field › Display only one Flexible Content Field at random › Reply To: Display only one Flexible Content Field at random
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;
//Count rows
$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"):
endif;
//If they do equal, add 1 to $flexible_row_number;
$flexible_row_number++;
else :
//If they dont equal, add 1 to $flexible_row_number;
$flexible_row_number++;
endif;
endwhile;
endif;
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!
Our guide looks into WordPress custom fields and how they stack up against the possibilities of ACF.
— Advanced Custom Fields (@wp_acf) July 18, 2022
https://t.co/hk3yibkHyk
© 2022 Advanced Custom Fields.
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.