Detailed explanation of function meaning
Call the header.php file from the current theme. Is not it 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 code for function definitions. Later, when I looked through it myself, I found that this habit was not very good, so I decided to post the function topic as long as the space allows. Convenient to browse by yourself.
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'); }
The use of get_header function
<?php get_header( $name ); ?>
is very simple. From the function declaration above, we can also see that the function only accepts one variable as a parameter.
Parameter explanation
$name, from the function declaration above, we can see that $name is a string variable used to call the header’s alias template,
For example, $name = “ab”;
That’s what we do
<?php $name = “ab” get_header( $name ); ?>
This will call the header-ab.php file as the header file call.
Example:
1. Simple 404 page
The following code is a simple template file specifically used 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. Multiple headers
Display different headers for different pages
<?php if ( is_home() ) : get_header( 'home' ); elseif ( is_404() ) : get_header( '404' ); else : get_header(); endif; ?>
These headers prepared for home and 404 should be named header-home.php and header respectively -404.php.
The above has introduced a detailed explanation of the usage of get_header to obtain the header function in WordPress development, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.