I have two variables in an external file
function.php
$bottomID = get_post_meta($post->ID, "bottom", true);
$xstring = "This is a string";
now if I echo them from my index.php
echo $bottomID;
echo $xstring;
I only get the value from $xstring
but not from $bottomID
I know that $bottomID
works since if I have it in the index.php
file it echo out a value.
Cant figure out whats the problem
Any ideas?
If you set a variable in
function.php
it is in the global scope, variable will be visible inindex.php
only because they are loaded in the same scope, but it is not available to all of your templates. Most templates are loaded by a function, and in PHP any variable used inside a function is by default limited to the local function scope, so you must explicit define a variable as global.In your case, variable is set and the value is
false
( test with:var_dump( isset( $bottomID ) );
inindex.php
), that’s because you use aglobal $post
, that not yet exists, as parameter in theget_post_meta()
function, so the return value of that function isfalse
.I would write a function in
functions.php
and call it inindex.php
.And in
index.php
: