0

We reuse allot of our ACF groups on different pages on our site. Is there a anyway to only "program" the backbone of it once and importing it on my other page templates? Right now I need to copy paste the code and if I want to change anything I need to go through all the pages that are using this ACF group and change the code.

Is it clear what I mean? Is this possible?

Thanks!

1

You could make one php file (not a page template) for the code of that group. Then you can include that file on the pages so you only have to edit one file to indirectly edit all the files that included that file.

You can do this with the get_template_part function

For example: You create a php file with the name of the group or something to remember it by like normalpage-code.php. you can place it directly in your (child) theme or create another directory and place it there. then in your page templates you can do:

<?php get_template_part( 'normalpage','code' ); ?>

or if you have it in another directory in your theme:

<?php get_template_part( '/anotherdirectory/normalpage','code' ); ?>

You could also use the php include function combined with locate_template to pass variables from the page template to the code for the group.

<?php include( locate_template( '/anotherdirectory/normalpage-code.php' ); ?>
  • Thanks, I think I understand. I made another folder directly in my themes root. named acf-templates. In that folder I put a file: ACF-hero-section.php where I copied my ACF code. Now I go to a page template located in theme/pages/my-page.php and in that file I put: <?php get_template_part( 'ACF', 'hero-section' ); ?>. This did not work though. I know that there is something I am missing, especially how to bind my file to the get_template_part function. What should I put as arguments in get_template_part() function? Instead of <?php get_template_part( 'ACF', 'hero-section' ); ?> – vonhact Oct 4 '17 at 7:55
  • I think I solved it. I put the dir name in the first arg as well, like such: <?php get_template_part( '/acf-templates/ACF', 'hero-section' ); ?> – vonhact Oct 4 '17 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.