Chris Andrews
asked on
wordpress, trying to pass a php variable to sidebar.php (called through a function)
On my single.php in my theme, I run a php script that ends up with two variables, like this:
$test1='test1';
$test2='test2';
I need to pass that information to the sidebar.php. sidebar.php is called and positioned into the single.php page like this:
<?php get_sidebar(); ?>
and apparently the php variables are not passed to it. If I try to echo them, I get no results.
Thanks for any help, Chris
$test1='test1';
$test2='test2';
I need to pass that information to the sidebar.php. sidebar.php is called and positioned into the single.php page like this:
<?php get_sidebar(); ?>
and apparently the php variables are not passed to it. If I try to echo them, I get no results.
Thanks for any help, Chris
ASKER
Hi jmiller1979, just tested and that's still not passing it to the sidebar.
hi,
can you better explain how your single.php is running...
when you say you run a script, do you mean inline php as part of single.php or do you mean you run a separate script with something like curl?
As long as you set $test1 and $test2 before including the sidebar then those variables should be available to the sidebar. From wordpress site,
get_sidebar: Includes the sidebar.php template file from your current theme's directory. If a name ($name) is specified then a specialized sidebar sidebar-{name}.php will be included.
So you need to set $test1 and $test2 before/above where the sidebar is included.
can you better explain how your single.php is running...
when you say you run a script, do you mean inline php as part of single.php or do you mean you run a separate script with something like curl?
As long as you set $test1 and $test2 before including the sidebar then those variables should be available to the sidebar. From wordpress site,
get_sidebar: Includes the sidebar.php template file from your current theme's directory. If a name ($name) is specified then a specialized sidebar sidebar-{name}.php will be included.
So you need to set $test1 and $test2 before/above where the sidebar is included.
ASKER
It' just inline.
So if I do this in single.php:
<? $test="test"; ?>
<?php get_sidebar(); ?>
Then put in the sidebar.php:
<? echo $test; ?>
the text 'test' should display on the post page, but it doesn't... or do I need to do something else to make it work?
So if I do this in single.php:
<? $test="test"; ?>
<?php get_sidebar(); ?>
Then put in the sidebar.php:
<? echo $test; ?>
the text 'test' should display on the post page, but it doesn't... or do I need to do something else to make it work?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ah, yes, that works.
Interesting... I don't understand the global setting very much, other than it's supposed to make the variable globally accessable I guess... but I would have thought I would have had to declare that it was global in the single.php. It seems like if it was passed to sidebar.php (which apparently it is since it can be accessed that way), it would show up without declaring it as global at that point.
Interesting... I don't understand the global setting very much, other than it's supposed to make the variable globally accessable I guess... but I would have thought I would have had to declare that it was global in the single.php. It seems like if it was passed to sidebar.php (which apparently it is since it can be accessed that way), it would show up without declaring it as global at that point.
yeah, global in php works kind of oppsosite than that. You tell your variable when you declare it, that you actually want access to the variable of the same name in the global scope, not that you are declaring a new global variable.
ASKER
It works though! Thank you very much!
<input type="hidden" name="test1" id="test1" value="<?php echo $_POST["test1"]; ?>" />
<input type="hidden" name="test2" id="test2" value="<?php echo $_POST["test2"]; ?>" />
Then on the page where you want to use them:
$test1 = $_POST["test1"];
$test2 = $_POST["test2"];
Check out the php manual for details.
http://www.php.net/manual/en/language.variables.external.php