I’m curious that why the get_header() or get_footer() function does not show output if called twice in the same .php file??

enter image description here

enter image description here

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?

2 Answers
2

The 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.

Leave a Reply

Your email address will not be published. Required fields are marked *