0

Say I have a file named content-header.php and content-body.php.

In content-header.php I have this variable:

$some_var = 'apples';

and in content-body.php I have a variable with the same name but different value:

$some_var = 'bananas';

In another file I use:

get_template_part('content', 'header');
get_template_part('content', 'body');

Would $some_var be limited to its own file or overwrite the other? Is it ok to use same variable names in files added through get_template_part()?

Would I have to use unique variable names?

3

You can absolutely use the same variable names. Arguments like that do not pass through from one template to another without a little help from a function.

So if in content-header.php you have $fruit = banana; and in content-body.php you have $fruit = apple; you will not have a conflict. Go nuts. Or bananas. ;)

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.