Replace the_content with ACF Flexible Content via function

function replace_the_content($content) {
    global $post;

    // ACF post summary
    if (get_field('post_summary')) :
      $post_summary = 'h4 ' . get_field('post_summary') . '/h4';
    endif;    

    // ACF More information
    if( have_rows('more_information') ):
      while( have_rows('more_information') ): the_row(); 

        $more_info = '
            div
              div'.get_sub_field('column_1') .'/div
              div'.get_sub_field('column_2') .'/div
            /div
        ';
      endwhile;
    endif;

    // To replace the_content
    $the_post = '

        div
          '.$post_summary.'
        /div
        
        div
          '.$more_info.'
        /div
    ';

    return $the_post;

    }
    
    return $content;
}

add_filter('the_content', 'replace_the_content');

This works, but now I want to get an ACF Flexible Content field in there, like so:

  if( have_rows('content_blocks') ):
    while ( have_rows('content_blocks') ) : the_row();

      if( get_row_layout() == 'text' ):
          $text = get_sub_field('text');
      elseif( get_row_layout() == 'image' ): 
          $file = get_sub_field('image');
      endif;

    endwhile;
  endif;   

I can't do this:

$acf_flex = '
  if( have_rows('content_blocks') ):
    while ( have_rows('content_blocks') ) : the_row();

      ...

    endwhile;
  endif;  
'

 $the_post = '

  div
    '.$post_summary.'
  /div

  div
    '.$acf_flex.'
  /div

';

How might I get those fields inside the $the_post variable?

Topic advanced-custom-fields the-content functions plugins Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.