<?PHP
include('wp-blog-header.PHP');
get_header();
?>
...Hello World...
<?PHP get_footer(); ?>
好吧,它工作得很好……但是标题和其他元标记的内容都是空的.如何在get_header()部分中更改标题内容或使用我自己的$变量?它不像这样工作:
$test="Blabla";
get_header();
.. inside a wordpress header template:
echo $test;
$test变量是空的:( ..有什么想法吗?谢谢!
解决方法:
$test变量是空的,因为函数包含了标题,因此有效地“插入”了函数,更重要的是,在不同的范围内..想想它就像
function get_header()
{
$test = '1234';
}
get_header();
echo $test; // won't work because test is in a different scope
但是你可以使用全局变量或$_SESSION变量,或者创建一个静态类来保存可以从任何地方调用的变量.
全局选项可能是这里最快的解决方案(不一定是最严格的).
$GLOBALS['test'] = "Blabla";
get_header();
.. inside a wordpress header template:
echo $GLOBALS['test'];
希望有所帮助