<Slushman />

unsplash-logo Adi Goldstein

How to Pass Variables with get_template_part

Published Jan 11, 2017

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 the most recent posts:

How to Center in CSS

This is the ultimate guide to centering elements like images, text, and just about anything else using CSS.