Slushman

unsplash-logo Adi Goldstein

How to Pass Variables with get_template_part

Published 2017-01-11

I’ve had several occasions where I needed to use variables in a file called using get_template_part, like one of the files in the template-parts folder in _s. It took a while for me to find the proper answer, so I’m documenting it here. Just before you use get_template_part(), add set_query_var(). As the arguments, add the name of the variable, then the value. Like:

set_query_var('user_id', $user_id);

In the template file, you can then use $user_id as you would any other variable.

Share this post!

Check out these most recent posts:

How to Add a Class to a Metabox

Toggling the visibility of a metabox using a form requires the metabox to have a custom class. This is how to add custom classes to a metabox.

ArtistDataPress Expiration

In case you haven’t heard, ArtistData is shutting down, so the ArtistDataPress plugin will also be shutting down as of October 14.

Avoiding get_theme_mod Errors

If you’re getting an error related to the get_theme_mod function, the solution is simpler than you think. Check out this solution.