Home Forums Add-ons Flexible Content Field Display only one Flexible Content Field at random


Display only one Flexible Content Field at random

  • Is there a way to randomly pick a single duplicate of a Flexible Content Field to display, so each time the user refreshes his/her page they’re presented with a unique set of data?

    I’m guessing this may be down to some form of PHP but I’m not sure about where to start.

    Many thanks in advance for any help or suggestions!

  • Hi guys

    Yes, the repeater field example can also be used for the flexible content field!


  • Brilliant, thanks a lot guys!

    Much obliged!

  • 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"):
                            //If they do equal, add 1 to $flexible_row_number;
    		else :
    			//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.