Support

Account

Home Forums Add-ons Flexible Content Field Check if end of loop or next row layout's name

Solved

Check if end of loop or next row layout's name

  • Hi there,

    I need to close a <div> if the next row in the loop is using a certain layout or if it#s the end of the loop.

    Explanation:
    I have two layouts: Layout A outputs a <div class=”row”> with a content of 100% width. Closing that row is easy.

    The second layout B however opens a <div class=”row”> and adds columns of 33% width to it. I now need a solution for the following thought:
    “if the next row layout is “Layout A” or the last row in the loop, then add </div>”

    Any help on this would be greatly appreciated.

    thanks!

  • if you do this

    
    $layouts = get_field('field_name', false, false);
    

    I think that $layouts should be an array. It’s length will tell you how many layouts there are and you can set up a counter to keep track of where you are and if you’re at the end. Having the list of layouts you can also look ahead in the array to see what the next layout is.

  • I´m afraid I´m not php savvy enough but will try that.
    Thanks a lot!

  • 
    $layouts = get_field('field_name', false, false);
    $count = 0;
    while (have_rows('flex_field')) {
      the_row();
      
      // normal code for displaying your layout here
    
      if ($count == count($layouts)) {
        // end of layouts reached
        // do end of loop code
      }
    
      if (isset($layouts[$count+1])) {
        if ($layout[$count+1] == 'Layout B') {
          // next layout is layout be
          // do that code
        }
      }
    
      // increment count
      $count++;
    
    } // end while have rows
    
  • John..thumbs up! 🙂
    Had to still try a lot but it worked in the end. Huge huge thanks!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Check if end of loop or next row layout's name’ is closed to new replies.