And to add to previous answers, you can not only use get_template_part
, but also use it’s second param and combine it with ACFs get_row_layout
.
So let’s say that somewhere in your template is:
<?php while ( have_posts() ) : the_post(); ?>
<article>
... SOME CODE
<?php while ( have_rows( 'YOUR_FIELD_NAME' ) ) : the_row(); ?>
... HERE SHOULD GO THE CODE FOR ACF FIELD
<?php endwhile; ?>
... SOME OTHER CODE
</article>
<?php endwhile; ?>
You can change it to:
<?php while ( have_posts() ) : the_post(); ?>
<article>
... SOME CODE
<?php
while ( have_rows( 'YOUR_FIELD_NAME' ) ) :
the_row();
get_template_part( 'block', get_row_layout() );
endwhile;
?>
... SOME OTHER CODE
</article>
<?php endwhile; ?>
Then you can create your custom templates named: block.php
(it will be used as fallback), block-layout_1.php
(it will be used for layout called “layout_1”, and so on.