setting variable in header.php but not seen in foo

2019-04-04 03:41发布

in wordpress , i set a variable in header.php

<?php
$var= 'anything'
?>

but in footer.php when I echo it

<?php
echo $var;
?>

I got no thing printed ... why !>

标签: wordpress
5条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-04 04:02

I know this is a bit old question and with a solution voted but I though I should share another option and just found a better solution (that works) without using 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
查看更多
小情绪 Triste *
3楼-- · 2019-04-04 04:05

You're not in the same scope, as the header and footer files are included in a function's body. So you are declaring a local variable, and referring to another local variable (from another function).

So just declare your variable as global:

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

Then:

echo $GLOBALS[ 'var' ];
查看更多
男人必须洒脱
4楼-- · 2019-04-04 04:09

In wordpress Header, any template, Footer is different functions so you have to declare any varible as a global variable then you can access it .

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

/** Template.php or Footer.php **/
<?php 
echo $xyz; ///return 123456
?>
查看更多
仙女界的扛把子
5楼-- · 2019-04-04 04:24

I know you've already accepted the answer to this question; however, I think there's a much better approach to the variable scope problem than passing vars into the $GLOBALS array.

Take the functions.php file in your theme for example. This file is included outside the scope of the get_header() and get_footer() functions. In fact it supersedes anything else you might be doing in your theme (and I believe in the plugin scope as well--though I'd have to check that.)

If you want to set a variable that you'd like to use in your header/footer files, you should do it in your functions.php file rather than polluting $GLOBALS array. If you have more variables that you want to sure, consider using a basic Registry object with getters/setters. This way your variables will be better encapsulated in a scope you can control.

Registry

Here's a sample Registry class to get you started if:

<?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()
    {
    }


}

Save this class in your theme somewhere, e.g. /classes/registry.class.php Include the file at the top of your functions.php file: include( get_template_directory() . '/classes/registry.class.php');

Example Usage

Storing variables:

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

Retrieving variables:

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

The registry will accept any variable type.

Note: I normally use SplObjectStorage as the internal datastore, but I've swapped it out for a regular ole array for this case.

查看更多
Root(大扎)
6楼-- · 2019-04-04 04:25

try this code

first define your initial variable

  $var="something";

then use the $_GLOBALS

  $_GLOBALS['myvar']=$var;

and finally use the global variable in anywhere you want

  global $myvar;

define string inside the $_GLOBALS as taken as global variable name or use the $_GLOBALS['myvar'] direct into the code without using the global

查看更多
登录 后发表回答