Wordpress-定义全局变量的最安全方法 [英] Wordpress - Safest method to define global variables
问题描述
我不知道全局"一词是否正确,但是我要做的是:定义一个可以在模板的许多部分中使用的变量.
I don't know if "global" is the right word, but what I have to do is: define a variable that I can use in many parts of the template.
示例:我必须定义一个包含静态页面ID的变量
Example: I have to define a variable that contains the ID of a static page
$page_id = 34
我必须在不同的模板部分中使用它,并使用
and I have to use it in different template parts, with functions like
get_page_link($page_id)
我找到了很多方法(在function.php中,PHP define(),全局,...),但是我问什么是最安全的?
I found many way to do it (PHP define(), in function.php, global, ...) but I ask what is the most secure, in your opinion.
推荐答案
您可以在wp-config
文件(可以在所有主题中访问)中定义它,也可以在主题目录的function.php
中定义它.
You can either define it in wp-config
file (which will be accessible among all theme ) or in your function.php
of your theme directory.
示例: Function.php
global $blog_pageId;
$blog_pageId = 34;
或者只是创建一个返回ID的小函数.
Or just create a small function which return you the id.
function get_blog_page_id(){
return "34";
}
可以称呼它为
$blog_pageId = get_blog_page_id();
这篇关于Wordpress-定义全局变量的最安全方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!