如何在wordpress模板中使用自己的php变量? [英] How to use own php variables in wordpress template?

查看:32
本文介绍了如何在wordpress模板中使用自己的php变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using a wordpress template in php like this:

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

...Hello World...

<?php get_footer(); ?>

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

ok, it works good... but the contents of title and other meta tags are empty. How can I change the header contents or use my own $variables inside the get_header() section? it doesnt work like this:

$test="Blabla";
get_header();

.. inside a wordpress header template:
echo $test;

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

the $test variable is empty :(.. any ideas? thanks!

推荐答案

$test 变量为空,因为标头包含在函数中,因此有效地在"函数中,更重要的是,在不同的作用域中.像这样想

The $test variable is empty because the header is included by a function, hence effectively 'in' the function, and more importantly, in a different scope.. think of it like

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

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

you can however use globals, or $_SESSION variables, or create a static class to hold variables in that can be called from anywhere.

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

the global option is probably the quickest fix here (not necessarily the strictest though).

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

.. inside a wordpress header template:
echo $GLOBALS['test'];

希望有帮助

这篇关于如何在wordpress模板中使用自己的php变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆