Reuse Variable across Wordpress Templates
I query some information in my wordpress blog's header. 开发者_运维问答How can I reuse the variable say in the sidebar of the blog?
If I try something like this in the sidebar, the output stays blank:
<?php if(isset($my_header_variable)) echo $my_header_variable; ?>
Just make the variable GLOBAL
and it will be available.
1) In your header.php:
<?php
$GLOBALS['skt'] = 44;
?>
2) In your sidebar.php:
<?php
echo $GLOBALS['skt'];
?>
And the value 44 will be correctly displayed. I use this way and it works fine for me.
If the value is not going to change you can use define
define("MY_VAR", "something");
Then to access later on
if(defined("MY_VAR"))
echo MY_VAR; // Will echo 'something'
精彩评论