admin管理员组

文章数量:1122832

Can I use the following function twice?

<php get_header(); ?>


<php get_header(); ?>


 <div class="news-heading">
        <span>Trending</span>
      </div>

<?php get_footer(); ?>

Please share the logic behind it..

Can I use the following function twice?

<php get_header(); ?>


<php get_header(); ?>


 <div class="news-heading">
        <span>Trending</span>
      </div>

<?php get_footer(); ?>

Please share the logic behind it..

Share Improve this question edited Oct 16, 2024 at 10:15 Rup 4,3904 gold badges28 silver badges29 bronze badges asked Oct 16, 2024 at 6:48 Ashutosh Kumar Singh RajbharAshutosh Kumar Singh Rajbhar 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

No, because get_header() calls locate_template() with load_once = true, meaning the actual template PHP is included with require_once

    if ( $load_once ) {
        require_once $_template_file;
    } else {
        require $_template_file;
    }

so the second time you call it it won't load anything, as it's already loaded the same file before.

This makes sense for header because typically header includes the the html, head and body tags to set up the page - you wouldn't want to include it twice. Documentation here.

(If you meant is it safe to accidentally call get_header() twice then yes it is, the second call won't do anything. But you shouldn't be calling it twice no.)

If you have a separate block that you do want to include twice you can use get_template_part().

本文标签: theme developmentWordPress function getheader()