Support

Account

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;