21Aug

WordPress passing variables to get_template_part()

I was using advanced custom fields and the flexible content (and clone fields). To keep the code easy to maintain I have some of the flexible content section in their own file. I needed to pass variables to those files.

How to pass variables to get_template_part()

Well, you can’t pass variables to get_template_part() but you can use include(locate_template(‘your-template-to-include.php’));

Update your code from something like:

get_template_part(template-parts/form);

to

include(locate_template('template-parts/form.php'));

This way all the variables in your page are also available in this template include.

Hope that helps!

Subscribe
Notify of

0 Comments
Inline Feedbacks
View all comments