I have created an archive page that will display the whole content of list of posts. The individual post content output should be managed from a specialized that I will integrate using the WP built-in get_template_part function. (I assume this is the best way to work).

My challenge is that I do need to get access to some variable which scope has been defined in the archive page. How do I access to them?

Should I make them "global"?

  • One thing you could do is globalize the variables is they're static. Another option is to just not use get_template_part but instead a conditional to decide whether to use this format or that format.
    – Howdy_McGee
    Commented Apr 24, 2014 at 21:59
  • 1
    What variable? And how is it used in the template-part file markup? Commented Apr 24, 2014 at 23:16
  • @Howdy_McGee not sure I can get the bit about the condition. Commented Apr 25, 2014 at 7:00

1 Answer 1


Don't use get_template_part. Instead use

include( locate_template( 'your-template-part.php' ) );

This will make your variables available to the included file without having to use the global keyword.

  • So in my archive.pho page, I will add a line like the above and the variable defined in archive will be immediately accessible to the content.php (my single item file). Is this correct? Commented Apr 25, 2014 at 7:02
  • That is correct
    – vancoder
    Commented Apr 25, 2014 at 16:03

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.