I have included the line below in my functions.php
file (at the top, also tried that line at the bottom of the code). 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. 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
global $something;
echo $somtehing;
in the header.php shows the value inside the wordpress header. global $something
is not needed in template files though. Can please anyone explain me why?
https://secure.php.net/manual/en/reserved.variables.globals.php
Thanks!
I think you can create a shortcode and echo shortcode to 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]'); ?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.