admin管理员组文章数量:1313091
I'm curious that why the get_header() or get_footer() function does not show output if called twice in the same .php file??
I tried to call the get_header() twice in my single.php file it runs fine but on the front end it shows output only once, what is the reason?
I'm curious that why the get_header() or get_footer() function does not show output if called twice in the same .php file??
I tried to call the get_header() twice in my single.php file it runs fine but on the front end it shows output only once, what is the reason?
Share Improve this question asked Dec 1, 2020 at 20:19 Rishi MalviyaRishi Malviya 313 bronze badges2 Answers
Reset to default 3The actual templates are loaded with require_once
, so PHP automatically ignores the second attempt to load them. (You will trigger the 'get_header' hook twice though.)
Here's the relevant code in get_header():
$templates[] = 'header.php';
if ( ! locate_template( $templates, true, true, $args ) ) {
return false;
}
The third parameter to locate_header(), the second true, is the load-with-require-once flag. There's no filter we can use to change that behaviour here.
Note that you could include two different header templates, e.g. if you created a copy of header.php as header-duplicate.php in your theme then
get_header();
get_header('duplicate');
would include them both, as this wouldn't get blocked by require_once.
The reason for this is simple:
It's not ment to be called twice!
The get_header() function loads all necessary scripts and styles for the page to be fully working. That also means all plugin files, and all Hooks or Filters that are ment to be loaded for the header.
Though I have never tried this, I guess that WordPress is skipping all scripts, when it is being called a second time. You cannot include twice for instance.
Same goes for the get_footer() function.
Quote from core docs:
Includes the header template for a theme or if a name is specified then a specialised header will be included.
For the parameter, if the file is called "header-special.php" then specify "special".
Meaning, that you may load a different part of your header, in case you need to.
If you want to include some scripts, use wp_enqueue_scripts(). If you want to include some different template parts, use get_template_part().
本文标签: functionsWhy getheader() or getfooter() does not run twice if called in the same php file
版权声明:本文标题:functions - Why get_header() or get_footer() does not run twice if called in the same php file? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741947352a2406499.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论