如何在wordpress模板中使用自己的php变量?

Mil*_*ike 2 php wordpress

我在php中使用wordpress模板,如下所示:

<?php
include('wp-blog-header.php');
get_header();
?>

...Hello World...

<?php get_footer(); ?>
Run Code Online (Sandbox Code Playgroud)

好吧,它工作得很好......但是标题和其他元标记的内容都是空的.如何在get_header()部分中更改标题内容或使用我自己的$变量?它不像这样工作:

$test="Blabla";
get_header();

.. inside a wordpress header template:
echo $test;
Run Code Online (Sandbox Code Playgroud)

$ test变量是空的:( ..有什么想法吗?谢谢!

nat*_*han 6

$ test变量是空的,因为函数包含了标题,因此有效地"插入"了函数,更重要的是,在不同的范围内..想想它就像

function get_header()
{
  $test = '1234';
}
get_header();
echo $test; // won't work because test is in a different scope
Run Code Online (Sandbox Code Playgroud)

但是你可以使用全局变量或$ _SESSION变量,或者创建一个静态类来保存可以从任何地方调用的变量.

全局选项可能是这里最快的解决方案(不一定是最严格的).

$GLOBALS['test'] = "Blabla";
get_header();

.. inside a wordpress header template:
echo $GLOBALS['test'];
Run Code Online (Sandbox Code Playgroud)

希望有所帮助