Support

Account

Home Forums Add-ons Repeater Field Why we need check have_rows('parent_field') two times?

Solved

Why we need check have_rows('parent_field') two times?

  • Hi!
    Thank you for the great plugin.

    I has looked the documentation and I don’t understand this:

    <?php 
    if( have_rows('parent_field') ): // Why we need this if-expression?
    
        while( have_rows('parent_field') ) : the_row(); // We're checking same at second time...
            $value = get_sub_field('sub_field');
        endwhile;
    
    endif;
    ?>

    And that I saw much times in the Docs.

  • For the same reason that in WP you do

    
    if (have_posts()) {
      while(have_posts()) {
        the_post();
      }
    }
    

    What if you want to do something outside the while loop but only if there are rows? What if you want to do something different if there are not rows?

    
    if (have_rows('field')) {
      // do something here before the loop
      while(have_rows('field')) {
        the_row();
      }
      // do something here after the loop
    } else {
      // do something different if there are not rows
    }
    

    Just like you can skip if (have_posts()) you can skip if(have_rows()) if you’re sure you don’t care what happens if there are none.

  • I see. With comments “// do something here”, the code looks better. It is clear that this is optional.

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

The topic ‘Why we need check have_rows('parent_field') two times?’ is closed to new replies.