If you’re doing everything right when developing a WordPress theme then this tutorial should only be of use to you once in a blue moon, but sometimes you just need to pass a variable through to another template part (for whatever your reason is).
I had to do this in our MyFolio WordPress Theme, which required me to find a post format, and dynamically load content depending on that format, so I’d normally do this using;
get_template_part();
http://codex.wordpress.org/Function_Reference/get_template_part
The only issue here, is that, as my title suggests, what if you need to pass a variable through? Well you could try using;
locate_template();
http://codex.wordpress.org/Function_Reference/locate_template
Again though, you’re not going to be able to pass variables through. So here’s the solution;
include(locate_template('file-name.php'));
Now, as I opened this post up with, if you’re doing everything correctly, there should be very few times that you need to do something like this, in fact get_template_part should have you covered 99 times our of 100. Using this method to pass a variable through files should only be used if you really need it, if you’re doing something a little out of the ordinary.
About The Author: Tommus
More posts by tommus