Support

Account

Home Forums Add-ons Flexible Content Field while ( have_rows() ) within while ( have_rows() ) …possible? Reply To: while ( have_rows() ) within while ( have_rows() ) …possible?

  • If your jump menu is number based and does not show any of the content from each block then you can simply get the number of rows in the field.

    
    $block_count = count(get_field('blocks'));
    

    If you’ll be showing content in the jump menu from each block, for example, if each block has a title and you need that title to be part of the menu, then you will need to loop through the rows twice. The first time gathering information you need for the menu and the second time to display each block.

    
    $blocks = array();
    if (have_rows('blocks')) {
      // first loop
      while (have_rows('blocks')) {
        the_row();
        $blocks[] = get_sub_field('title');
      }
      // second loop
      while (have_rows('blocks')) {
        the_row();
        // output the row
      }
    }
    

    There are also other ways you can do this, for example, if you use

    
    $blocks = get_field('blocks');
    // blocks holds nested array of all fields and subfields
    echo '<pre>'; print_r($blocks); echo '</pre>';
    

    Using this array you could build a nested loop the way you want to build it because nested foreach loops can reference the same array parameter and they will loop separately.

    
    foreach ($array as $index => $value) {
      // this will happen as many times as there are array elements
      foreach ($array as $nested_index => $nested_value) {
        // this will happen {number of array elements} squared
      }
    }
    

    Hope some of this information helps.