Passing Variables Through get_template_part()

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.

7 Comments

  1. I found this post when googling for passing a variable to get_template_part in WordPress. The example in the post here didn’t work for me as I got an “Undefined variable” error back. What really would help is a short code example that show how you pass a variable. Does it need to be a global?

  2. How the fuck is passing a variable to a template something ‘out of the ordinary’ that you hardly ever need to do?!

    It’s comments like this that really don’t make wordpress fun for people coming from an MVC background!

  3. Unfortunately this is a known limitation and doesn’t appear to be getting addressed within WP core.

    I agree with you, developed a large number of WP sites / apps and just ran into this…

    I disliked implementing your solution, as its not a native WP method. BUT it is effective.. thanks for thinking outside the box

    1. No problem! The good news is that locate_template() is child theme compatible, so not quite as horrible as it seems, but certainly not nice to look at an include() like that!

  4. Couldn’t you just use “global $variable;” in your template part? At least for me, this is working.

    Example:
    In your index.php:


    $templVar = “Example passing of variable.”;
    get_template_part(‘content’, ‘example’);

    and in your template content-example.php:


    global $tmplVar;
    echo $tmplVar;

    1. Indeed you can use globals, but realistically you want to keep global usage to a minimum to avoid conflicts with plugins etc. which is why locate_template() is a more preferred solution in this case.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.