简体   繁体   English

在WordPress的functions.php中包含php文件不适用于header.php

[英]Include php file in Wordpress' functions.php not working for header.php

I have included the line below in my functions.php file (at the top, also tried that line at the bottom of the code). 我已经在我的functions.php文件中包含了以下行(在顶部,在代码底部也尝试了该行)。 I can access variables and all other stuff defined in that file in all templates . 我可以在所有模板中访问该文件中定义的变量和所有其他内容。 But when I try to echo $something from that file within the header.php , nothing shows up. 但是,当我尝试从header.php中的该文件中echo $something ,什么都没有显示。 No content, no error message. 没有内容,没有错误消息。

require_once '/path/to/file.php';

Ok, after lots of trying I just found out that using the line from above inside the functions.php with 好的,经过大量的尝试,我发现在functions.php中使用上面的代码

global $something;  
echo $somtehing;

in the header.php shows the value inside the wordpress header. header.php中的内容显示了wordpress标头中的值。 global $something is not needed in template files though. 模板文件中不需要global $something Can please anyone explain me why? 能否请任何人解释我为什么?

https://secure.php.net/manual/en/reserved.variables.globals.php https://secure.php.net/manual/zh/reserved.variables.globals.php

Thanks! 谢谢!

I think you can create a shortcode and echo shortcode to header.php https://codex.wordpress.org/Shortcode_API 我认为您可以创建一个简码并向header.php回显简码https://codex.wordpress.org/Shortcode_API

example

function foobar_func( $atts ){
    return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

and call in header 并调用标题

<?php echo do_shortcode('[foobar]'); ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM