Support

Account

Home Forums General Issues have_rows() problem showing data after data has been removed

Solved

have_rows() problem showing data after data has been removed

  • Hi all,

    Just recently i have been working on a website for a client and came across a problem displaying a header, but only when the field is populated.

    As you can see below i am using an h2 header inside the ‘if( have_rows )’ function. This is to display the header only if there is content in the sub_field.

    The problem is that in some instances it shows the header if the sub_field is empty, but only in certain circumstances.

    1. In the initial instance when the sub_fields are new and have not been used, the h2 does not display.

    2. When the sub_field/sub_fields are populated the h2 displays.

    3. But when the sub_field/sub_fields have been populated, but subsequently that data has been removed, the h2 still displays. Also, the ‘<div class=”column”>’ also still displays but with no content inside it.

    So my question is, am i doing something wrong, or is this the way it works?
    If the latter, then how do i get around the h2 and div displaying when the sub_fields have had their content removed?

    Hope someone can help.

    
    <div class="exhibitor_videos">
      <?php // Check rows exists.
        if( have_rows('exhibitor_videos') ):
          // Show header
          echo '<h2 class="entry-header">Video Gallery</h2>';
          // Loop through rows.
          while( have_rows('exhibitor_videos') ) : the_row();
            // Load sub field value.
            $exhibitor_video = get_sub_field('exhibitor_video');
            $exhibitor_video_title = get_sub_field('exhibitor_video_title');
              // Do something...
             	echo '<div class="column">';
                echo do_shortcode("[video src=" . $exhibitor_video . "]");
                echo '<p>' . $exhibitor_video_title . '</p>';
             	echo '</div>';
            // End loop.
          endwhile;
        else : // No value.
          // Do something...
        endif;
      ?>
    </div>
    
  • Have the fields just had content removed or have the rows been deleted. If the rows still exist but the fields are empty then the repeater still has rows. This is the only thing that I can see that could cause this.

    If this is the case what I would do is to use an output buffer and create a flag to tell me if any content exists in the sub fields.

    
    <?php 
          // add output buffer
          ob_start()
          // flag
          $has_content = false;
    ?>
    <div class="exhibitor_videos">
      <?php // Check rows exists.
        if( have_rows('exhibitor_videos') ):
          // Show header
          echo '<h2 class="entry-header">Video Gallery</h2>';
          // Loop through rows.
          while( have_rows('exhibitor_videos') ) : the_row();
            // Load sub field value.
            // add check for content
            if (get_sub_field('exhibitor_video')) {
              // update flag
              $has_content = true;
              $exhibitor_video = get_sub_field('exhibitor_video');
              $exhibitor_video_title = get_sub_field('exhibitor_video_title');
                // Do something...
               	echo '<div class="column">';
                  echo do_shortcode("[video src=" . $exhibitor_video . "]");
                  echo '<p>' . $exhibitor_video_title . '</p>';
               	echo '</div>';
            } // end if get field
            // End loop.
          endwhile;
        else : // No value.
          // Do something...
        endif;
      ?>
    </div>
    <?php 
      // get buffer
      $content = ob_get_clean();
      // only show content if there was content
      if ($has_content) {
        echo $content;
      }
    ?>
    
  • Thanks John, i see what you mean, but unfortunately that code gave my site an error “There has been a critical error on this website.”.

  • When developing a site you should turn on WP_DEBUG error display so that you can see the actual errors.

    The problem is that there is a missing semi-colon on my first line

    
    ob_start()
    

    should be

    
    ob_start();
    
  • Thanks John, I’m actually just tweaking the live site now, not working on a development site, that’s why i didn’t have the WP_DEBUG on.

    But you are a genius, that sorts the problem, and fixes my issue. Still strange that ACF works that way.

    Thank you for your help.

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

You must be logged in to reply to this topic.