Support

Account

Home Forums General Issues Is there a way to store shared variables in one place to reuse? Reply To: Is there a way to store shared variables in one place to reuse?

  • This has to do with variable scope. When you use get_template_part() your template is included inside of a function and only variables declared inside of that function are available to your template file.

    Besides the above you could use global variables.

    Another way would be do include your template using a php include instead and bypassing WP. Doing this any variables that are available in the calling template plus any variables you create in your calling template are available in your included file. Like everything, this has it’s ups and downs.

    Another possibility it to “include” the file that defines the common variables in each of your templates that are called using get_template_part() thus making them available to the calling file.