When we create a WordPress theme we some times need to pass a variable to another template for some functionality and save time, to reduce duplicate code purpose, but WordPress get_template_part() function does not support this functionality. This function doesn’t accept any other arguments to pass variables to template. For this requirement we have other solutions.

Method 1:
You can use locate_template() function in the place of get_template_part(), this function work with PHP include() function like as:

All of the variables available in your main template file will be available in that template file now.

Method 2:

Simply have to declare your variable as global:

you can call this variable on other template like as:

Method 3:

For example you want to pass $custom_var to the template part at “content-page.php” .

Follow like as:

Get value on content-page.php by using get_query_var()