Php 在Wordpress中,我将在哪里包含我的文件,以便在页面的其余部分访问其变量?
我尝试将我的文件包含在Php 在Wordpress中,我将在哪里包含我的文件,以便在页面的其余部分访问其变量?,php,wordpress,include,scope,Php,Wordpress,Include,Scope,我尝试将我的文件包含在header.php或主题的index.php中,但我无法从主题的footer.php甚至主题的页面模板访问包含文件中的变量 以下是我要介绍的内容: <?php // some parameters $var_research = 5; $var_researchtrans = 7; $var_output = 9; $var_edit_indi = 11; $var_contact = 15; $var_transition = 19; ?> 现在我
header.php
或主题的index.php
中,但我无法从主题的footer.php
甚至主题的页面模板访问包含文件中的变量
以下是我要介绍的内容:
<?php
// some parameters
$var_research = 5;
$var_researchtrans = 7;
$var_output = 9;
$var_edit_indi = 11;
$var_contact = 15;
$var_transition = 19;
?>
现在我需要的是能够使用footer.php中的变量,例如
希望有人能给出答案。谢谢大家
好的,我是这样做的:
在functions.php中
在footer.php中
它起作用了,但我做对了吗?有没有更好/正确的方法?再次感谢大家。我不确定您的函数是什么,但定义函数的位置是在themes functions.php文件中。包含您自己的函数的最佳位置是在themes的
functions.php
文件中
如果要访问多个文件中的变量,可以在functions.php
中创建一个函数,并在主题中的任何位置访问该函数
在functions.php中
function your_variable()
{
$var = 'your variable';
return $var;
}
echo your_variable();
和在footer.php中
function your_variable()
{
$var = 'your variable';
return $var;
}
echo your_variable();
用于更新的查询
嗯,这个问题的原始解决方案是您必须在wordpress中将您的VAR保存为选项变量,如下所示:
<?php add_option( $name, $value, $deprecated, $autoload ); ?>
以及功能:
<?php
function my_var($va_var) {
add_option( 'var_research', '5', '', 'yes' );
}
?>
在footer.php中,您只需使用以下内容检索变量:
<?php _e(get_option('admin_email')); ?>
祝你好运谢谢@Jeremy,我刚刚在问题中加入了代码。你建议我如何在函数中实现这一点?这又是一个更新,显示了我的答案,@Jeremy。有更好的方法吗?谢谢@Indranil,我刚刚在问题中加入了代码。我将尝试将它们转换为一个函数。有什么建议吗?还有一个更新来显示我的答案,@Indranil。有更好的方法吗?存储它们的更好方法是使用数组。请看我编辑过的答案。请记住,不要使用任何自制的解决方案,最好先查看法典中的原始解决方案;)
<?php _e(get_option('admin_email')); ?>