in wordpress , i set a variable in header.php
<?php
$var= 'anything'
?>
but in footer.php when I echo it
<?php
echo $var;
?>
I got no thing printed ... why !>
In the left-hand menu, go to Appearance -> Editor. Click on Editor. In the list of theme files on the right side, search for the file named Theme Footer (footer. php).
To find the file and edit it yourself go to wp-content > themes > your-theme-name > header. php. You can then open it in a code editor and make any changes you need to.
You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).
So just declare your variable as global:
$GLOBALS[ 'var' ] = '...';
Then:
echo $GLOBALS[ 'var' ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With