WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 7 down vote accepted

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


global $message; 
echo $message; 
share|improve this answer


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

Ie require(locate_template('messages'));

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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