在header.php中设置变量但在footer.php中没有看到

时间:2010-03-12 17:54:00

标签: wordpress

wordpress中的

 我在 header.php

中设置了一个变量
<?php
$var= 'anything'
?>

但在 footer.php 时,我回应它

<?php
echo $var;
?>

我没有打印出来......为什么!&gt;

5 个答案:

答案 0 :(得分:24)

您不在同一范围内,因为页眉和页脚文件包含在函数体中。所以你要声明一个局部变量,并引用另一个局部变量(来自另一个函数)。

所以只需将你的变量声明为全局:

$GLOBALS[ 'var' ] = '...';

然后:

echo $GLOBALS[ 'var' ];

答案 1 :(得分:10)

我知道你已经接受了这个问题的答案;但是,我认为对变量范围问题有一个更好的方法,而不是将变量传递到$GLOBALS数组。

以您主题中的functions.php文件为例。此文件包含在get_header()get_footer()函数的范围之外。实际上它取代了你在主题中可能做的任何事情(我也相信插件范围 - 虽然我必须检查它。)

如果要设置要在页眉/页脚文件中使用的变量,则应在functions.php文件中进行,而不是污染$ GLOBALS数组。如果您想要确定更多变量,请考虑使用带有getter / setter的基本Registry对象。这样,您的变量将更好地封装在您可以控制的范围内。

注册表

以下是一个示例Registry课程,可帮助您入门:

<?php
/**
 * Registry
 *
 * @author Made By Me
 * @version v0.0.1
 */
class Registry
{
    # +------------------------------------------------------------------------+
    # MEMBERS
    # +------------------------------------------------------------------------+
    private $properties = array();

    # +------------------------------------------------------------------------+
    # ACCESSORS
    # +------------------------------------------------------------------------+
    /**
     * @set     mixed   Objects
     * @param   string  $index  A unique index
     * @param   mixed   $value  Objects to be stored in the registry
     * @return  void
     */
    public function __set($index, $value)
    {
        $this->properties[ $index ] = $value;
    }

    /**
     * @get     mixed   Objects stored in the registry
     * @param   string  $index  A unique ID for the object
     * @return  object  Returns a object used by the core application.
     */
    public function __get($index)
    {
        return $this->properties[ $index ];
    }

    # +------------------------------------------------------------------------+
    # CONSTRUCTOR
    # +------------------------------------------------------------------------+
    public function __construct()
    {
    }


}

将此课程保存在您的主题中的某个位置,例如/classes/registry.class.php将文件包含在functions.php文件的顶部:include(get_template_directory()。'/ classes / registry.class.php');

使用示例

存储变量:

$registry = new Registry();
$registry->my_variable_name = "hello world";

检索变量:

echo '<h1>' .  $registry->my_variable_name . '</h1>'

注册表将接受任何变量类型。

注意:我通常使用SplObjectStorage作为内部数据存储区,但在这种情况下,我已将其换成常规的ole数组。

答案 2 :(得分:3)

我知道这是一个有点老问题并且已经投了一个解决方案,但是我应该分享另一个选项并且找到一个更好的解决方案(有效)而不使用Globals

function fn_your_var_storage( $var = NULL )
{
    static $internal;

    if ( NULL !== $var )
    {
        $internal = $var;
    }

    return $internal;
}

// store the value
fn_your_var_storage( 'my_value' );

// retrieve value
echo fn_your_var_storage(); // print my_value

答案 3 :(得分:0)

试试这段代码

首先定义你的初始变量

  $var="something";

然后使用$ _GLOBALS

  $_GLOBALS['myvar']=$var;

最后在任何你想要的地方使用全局变量

  global $myvar;

将$ _GLOBALS中的字符串定义为全局变量名称,或者使用$ _GLOBALS [&#39; myvar&#39;]直接导入代码而不使用全局

答案 4 :(得分:-1)

在wordpress Header中,任何模板,Footer都是不同的函数,因此您必须将任何varible声明为全局变量,然后才能访问它。

/** header.php **/
<?php
global $xyz;
$xyz="123456"; ?>

/** Template.php or Footer.php **/
<?php 
echo $xyz; ///return 123456
?>