![](/img/trans.png)
[英]What is wrong with this attempt using php to include functions.php and header.php file?
[英]Include php file in Wordpress' functions.php not working for header.php
我已经在我的functions.php
文件中包含了以下行(在顶部,在代码底部也尝试了该行)。 我可以在所有模板中访问该文件中定义的变量和所有其他内容。 但是,当我尝试从header.php中的该文件中echo $something
,什么都没有显示。 没有内容,没有错误消息。
require_once '/path/to/file.php';
好的,经过大量的尝试,我发现在functions.php中使用上面的代码
global $something;
echo $somtehing;
header.php中的内容显示了wordpress标头中的值。 模板文件中不需要global $something
。 能否请任何人解释我为什么?
https://secure.php.net/manual/zh/reserved.variables.globals.php
谢谢!
我认为您可以创建一个简码并向header.php回显简码https://codex.wordpress.org/Shortcode_API
例
function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
并调用标题
<?php echo do_shortcode('[foobar]'); ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.