Support

Account

Home Forums ACF PRO Repeater has_rows is false after a while loop

Solved

Repeater has_rows is false after a while loop

  • I have a small problem with an inlined PHP code for displaying 2 different divs. One of them has an if condition for a advanced control – repeater, and the other is a while loop for posts. When the if condition div is first, and then the while loop follow both are displayed. However, if i swap their place and put the while div on top the 2nd div (the if condition one) disappears, it’s has_rows is somehow false: Here are the 2:

    1. The If – div

    <div id="next"></div>
    <?php if( have_rows('products_grid') ):
      if( get_field('product_grid_header') ){
    
        $data = ['header' => get_field('product_grid_header'), 'img' => 'rectangle-proizvodi.png', 'url' => get_field('product_header_url')];
        include(locate_template('templates/elements/devider.php'));
      }?>
    
      <div class="products-grid white">
        <div class="container sweet-products-container">
          <div class="row">
            <div class="col-md-12">
              <div class="row nomargin">
                <?php while ( have_rows('products_grid') ) : the_row();
                        include(locate_template('templates/elements/products-grid-item.php'));
                    endwhile; ?>
              </div>
            </div>
          </div>
        </div>
      </div>
    
    <?php endif; ?>
    

    2. The while loop – div

    <div class="news product-news homepage">
      <div class="container">
        <div class="row">
          <div class="col-sm-12">
    
            <div class="owl-carousel products owl-theme">
    
              <?php
              $args = array(
              'post_type' => 'post',
              'post_status' => 'publish',
              'posts_per_page' => '5',
              'tax_query' => array(
                array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array ('top-vesti')
                )
            )
              );
              $my_query = new WP_Query( $args );?>
    
              <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                <article class="item">
                  <div class="row">
                    <div class="col-sm-12">
                      <?php if ( has_post_thumbnail() ) : ?>
                          <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                              <?php the_post_thumbnail('medium'); ?>
                          </a>
                      <?php endif; ?>
                      <div class="post-info">
                        <a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a>
                        <div class="article-excerpt">
                          <?php the_excerpt(); ?>
                          <a class="read-more" href="<?php the_permalink() ?>"><?php pll_e( 'Повеќе' ); ?></a>
                        </div>
    
                      </div>
                    </div>
                  </div>
                </article>
              <?php endwhile;  ?>
            </div>
          </div>
        </div>
      </div>
    </div>

    So if the order is like this IF then while, both are shown. But, if i swap them and put the while one first, the if one is not shown. Is there anything in the while loop that could cause has_row for the repeater to not have rows? Any suggestions are appreciated.

  • If the while over the posts is first, at the end WP has the last post of your loop in the global $post variable. You need to https://developer.wordpress.org/reference/functions/wp_reset_postdata/

  • I tried adding
    <?php endwhile; $my_query->reset_postdata(); ?>
    but this didn’t help. I’m not very familiar with WP, but maybe i should try
    <?php endwhile; wp_reset_postdata(); ?>
    instead. Please correct me if i’m doing something wrong 🙂

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

The topic ‘Repeater has_rows is false after a while loop’ is closed to new replies.