As far as I understand (from the get_template_part codex) get_template_part is just a wrapper round the PHP require function.

So if I have a variable that I have create in a page template file e.g. $message, I would have assumed you could directly use that variable in the template part

So in template file:

$message = 'my message';

Then in template part messages.php:

<?php echo $message; ?>

However the echo will display nothing.

3 Answers 3


D'oh, it just needs a global as its inside a function.


global $message; 
echo $message; 


You can use locate_template to find the template file to include. Bit cleaner than using globals.

Ie require(locate_template('messages'));


If you use locate_template() instead of get_template_part() you can use all variables in that script:

<?php include(locate_template('message.php')); ?>
<?php echo $message; ?>

Echo $message will work.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.