【问题标题】:setting variable in header.php but not seen in footer.php在 header.php 中设置变量但在 footer.php 中看不到
【发布时间】:2010-03-12 17:54:00
【问题描述】:

在 wordpress 中, 我在 header.php

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

但是在 footer.php 当我回显它时

<?php
echo $var;
?>

我没有打印任何东西...为什么!>

【问题讨论】:

  • 你试过注释掉他们之间的一切吗?我的猜测是一些无关的代码正在覆盖该变量。或者也许 echo 根本没有执行。或者变量设置可能不起作用。你给的还不够。你有什么错误吗?

标签: wordpress


【解决方案1】:

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

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

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

然后:

echo $GLOBALS[ 'var' ];

【讨论】:

    【解决方案2】:

    我知道您已经接受了这个问题的答案;但是,我认为对于变量范围问题,有一种比将 vars 传递到 $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.phpfunctions.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 数组。

    【讨论】:

    • 尽管这个反全局解决方案是高尚的,但在函数文件中声明的 $registry 变量仍然在页脚范围之外:(
    • ... 如果在 header.php 中声明,则相同 => 我刚刚实现了您的解决方案(将整个注册表类放在 functions.php 中),并且在 header.php 中声明的 $registry 变量是footer.php 中仍然为空/null :/
    【解决方案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
    

    【讨论】:

      【解决方案4】:

      试试这个代码

      首先定义你的初始变量

        $var="something";
      

      然后使用 $_GLOBALS

        $_GLOBALS['myvar']=$var;
      

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

        global $myvar;
      

      在 $_GLOBALS 中定义字符串作为全局变量名,或者直接在代码中使用 $_GLOBALS['myvar'] 而不使用全局

      【讨论】:

      • 不是 $_GLOBALS,应该是 $GLOBALS。
      • 对不起,我忘了删除下划线
      【解决方案5】:

      在 wordpress 中,任何模板的页眉、页脚都是不同的功能,因此您必须将任何变量声明为全局变量,然后才能访问它。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-05
        • 2012-02-22
        • 1970-01-01
        • 1970-01-01
        • 2019-04-19
        • 1970-01-01
        • 2015-10-13
        • 1970-01-01
        相关资源
        最近更新 更多