Last Updated: February 25, 2016
·
6.89K
· pimschaaf

Passing variables to get_template_part in WordPress

Use the WordPress locate_template() function within PHP’s include(), instead of get_template_part().

<?php
$var = 'Hello World!';
include(locate_template('file/path/filename.php'));
?>

In file/path/filename.php:

<?php
echo $var;
?>

returns:

Hello World!

Credits: Keith Devon