Include PHP Variables Inside Template Parts From The Outside

Trying to include PHP variable inside the get_template_part() function from the outside? All you may need is to use PHP globals inside your template part.

Include external PHP variables inside get_template_part()

To include PHP variables inside your template part, which are located outside; you can use PHP globals as such inside your template part:


$example_var = 123;
get_template_part( 'templates/example.php' );


global $example_var;
<div class="example">
     <?php echo $example_var; ?>