Below is my regular Flexible Content loop in my front-page.php template file. Since I want to re-use these flexible content layouts on other pages I’ll have to copy/paste the same loop code on other templates, which isn’t ideal DRY-wise. Instead I wish to have this code only once, probably in my functions.php file, and somehow hook it to the_content() on certain pages where that code would run in place of the_content().

Is that possible?

Flexible content loop code:

if (have_posts()):
  while (have_posts()) : the_post();
    if( have_rows('front_page_sections') ): 
      while ( have_rows('front_page_sections') ) : the_row();
        switch (get_row_layout()):
          case 'hero':
            get_template_part("templates/front_page_hero");
            break;
          case 'advantages_1':
            get_template_part("templates/front_page_advantages1");
            break;
          case 'general_presentation':
            get_template_part("templates/front_page_general_presentation");
            break;
        endswitch;
      endwhile;
    endif;
  endwhile;
endif;

Read more here: ACF: how to run Flexible Content loop on multiple pages from functions.php?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: