How do I declare a global variable in PHP I can use across templates?

前端 未结 5 1752
逝去的感伤
逝去的感伤 2020-12-29 02:47

In wordpress, it calls header.php then whatever template name or the page requested. How can I declare a variable in my php header which I can then refer to in my other tem

5条回答
  •  我在风中等你
    2020-12-29 03:11

    You can declare it like so (In functions.php):

    global $domain;
    $domain = 'http://www.mydomain.com/';
    

    You can echo the domain of the WordPress site like this, though..

    
    

    Or

    $url = site_url();
    echo $url;
    

    Or as a function, in functions.php

    function displayMyDomain() {
      global $domain; // you probably don't actually need to set it global as it is a function
      $domain = 'http://www.domain.com/';
      echo $domain;
    }
    

    Then anywhere, use

提交回复
热议问题