I have function in functions.php
function my_function() {
//do something
}
but inside this function i need to check page template and i need to use is_page(), or is_home() but in functions.php it didnt't work.
I can check page template at header.php, for example:
if (is_page(7)) :
$myVariable = 1;
else :
$myVariable = 2;
endif;
End it works at header.php, but now in functions.php in my_function i need something like this:
function my_function(){
if($myVariable == 1) :
//do something
else :
//do something else
endif;
}
I dont know how to pass this variable, or how to check page template in my function.
you can use the global keyword as follows.
if (is_page(7)) :
$myVariable = 1;
else :
$myVariable = 2;
endif;
function my_function(){
global $myVariable;
if($myVariable == 1) :
//do something
else :
//do something else
endif;
}
You can pass the variable into the function as a parameter like so:
function my_function($myVariable) {
if($myVariable == 1) :
//do something
else :
//do something else
endif;
}
or you can declare $myVariable
as a global variable like so:
function my_function() {
global $myVariable;
if($myVariable == 1) :
//do something
else :
//do something else
endif;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.