0

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 Apr 24 '14 at 21:59
  • 1
    What variable? And how is it used in the template-part file markup? – Chip Bennett Apr 24 '14 at 23:16
  • @Howdy_McGee not sure I can get the bit about the condition. – Andrea Moro Apr 25 '14 at 7:00
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? – Andrea Moro Apr 25 '14 at 7:02
  • That is correct – vancoder Apr 25 '14 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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