Detailed explanation of the meaning of the function
Call the header.php file from the current theme. Isn't it very simple? Well, if you are a newbie, I would like to remind you that the get here is slightly different from the get in get_children() and get_category.
get_header function declaration (definition)
In the past, when I wrote articles, I rarely wrote the code of function definitions. Later, when I read it, I found that this habit was not very good, so I decided that as long as the space allowed, I would post the function topic to make it easier for me to read it.
The get_header function is declared (defined) around lines 24 – 36 of the wp=include/general-template.php file.
function get_header( $name = null ) { do_action( 'get_header', $name ); $templates = array(); if ( isset($name) ) $templates[] = "header-{$name}.php"; $templates[] = 'header.php'; // Backward compat code will be removed in a future release if ('' == locate_template($templates, true)) load_template( ABSPATH . WPINC . '/theme-compat/header.php'); }
Usage of get_header function
<?php get_header( $name ); ?>
It’s very simple. From the function declaration above, we can also see that the function only accepts one variable as a parameter.
Parameter explanation
$name, as we can see from the function declaration above, $name is a string variable used to call the header’s alias template,
For example $name = “ab”;
That’s how we are
<?php $name = “ab” get_header( $name ); ?>
This will call the header-ab.php file as the header file.
Example:
1. Simple 404 page
The following code is a simple template file specifically designed to display "HTTP 404: Not Found" errors (this file should be included in your theme and named 404.php)
<?php get_header(); ?> <h2>Error 404 - Not Found</h2> <?php get_sidebar(); ?> <?php get_footer(); ?>
2. Various heads
Show different headers for different pages
<?php if ( is_home() ) : get_header( 'home' ); elseif ( is_404() ) : get_header( '404' ); else : get_header(); endif; ?>
These headers for home and 404 should be named header-home.php and header-404.php respectively.