php - Pass variable from header.php to functions.php
问题描述
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.
解决方案
您可以使用 global 关键字,如下所示。
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;
}
推荐阅读
- python - 在views.py中使用return render时我无法打开'about.html'
- python - “验证交易:失败”更新 anaconda 表单时出错 anaconda 提示
- javascript - 如何在 Redis 中存储大型多维数组?
- python - Python + OpenGL + FreeType .. 中文字符不起作用
- python - 为什么 Python 中会出现错误:SyntaxError: f-string: mismatched '(', '{', or '[' in a block of code?
- android - 在 RecyclerView 中未正确解析暗模式颜色
- google-sheets - Google 表格 - 动态更改单元格引用
- python - 正则表达式从匹配的下一个字符开始
- c# - 需要获取从 AutoCAD Civil3D 中的类创建的对象的计数
- notepad++ - 记事本++多个数字和字符交换