Support

Account

Home Forums Bug Reports get_row doesn't reset in a have_rows loop

Solving

get_row doesn't reset in a have_rows loop

  • I have code that looks like this:

    if( have_rows('content_sections') ) :
    
        while ( have_rows('content_sections') ) : the_row();
    
        $template = get_row_layout();
        $field = get_row(1);
    
        if( locate_template( "template-parts/acf/{$template}.php" ) ){
            include( locate_template( "template-parts/acf/{$template}.php" ));
        }
    
        endwhile;
    
    endif;

    It’s looping through a Flexible Content loop and loading a template file based on the get_row_layout(). The issue comes into play when there are 2 of the same field immediately after each other. In this case, I’m showing multiple galleries on a page and populating the template file with the row data gathered by calling get_row(1) formatted. The second gallery is populated with the all the data from the first gallery.

    Yet when I call get_sub_field() for each field within each row, that works fine. Is there some way to reset the row? I thought that’s what the_row() did?

    I should note, if I call get_row() raw, the row data is reset. It seems it’s only when $formatted=true is passed to get_row().

  • I could be wrong, but it looks to me like an issue with the cached value of the previous row. I’ve reported it to the developer.

    In the mean time could you try adding this before getting each row

    
    $post_id = $post->ID;
    $field_name = 'content_sections'
    acf_delete_cache("format_value/post_id={$post_id}/name={$field_name}");
    
  • Hi @darrinb

    Thanks for the bug report.
    I’ve just done some testing but can’t replicate the issue.

    What version of ACF are you using?
    Can you please export and attach your field group .json so I may test the same fields?

  • Hey sorry for the very late reply. I am still coming across this issue but it happens for me when I have a flexible field inside a repeater.

    if (have_rows('tab_content')){
      while(have_rows('tab_content')){
    
        $tabContent[$i]['tab_name'] = get_sub_field('tab_name');
        $tabContent[$i]['tab_description'] = get_sub_field('tab_description');
    
        if(have_rows('content')){
          while(have_rows('content')){
            the_row();
    	$currentLayout = get_row_layout();
    	$tabData['row'] = get_row(true); // Information in this is cached
    	$tabData['content_columns'] = get_sub_field('content_columns'); // Works if I directly call it like this
    	$tabContent[$i]['tab_content_layouts'][] = Render('page-flex__'.$currentLayout.'.twig', $tabData);
          }//end while
        }//end if
          $i++;
      }//end while
    }//end if
    //Render main with $tabContent
    $data['tab_content'] = $tabContent;
    Render('page-section-layout.twig', $data)
    

    I am using ACF Pro version 5.6.10

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

You must be logged in to reply to this topic.