Loads header template.
Description
Includes the header template for a theme or if a name is specified then a specialized header will be included.
For the parameter, if the file is called "header-special.php" then specify "special".
Parameters
$name
stringoptional- The name of the specialized header.
Default:
null
$args
arrayoptional- Additional arguments passed to the header template.
Default:
array()
Source
function get_header( $name = null, $args = array() ) {
/**
* Fires before the header template file is loaded.
*
* @since 2.1.0
* @since 2.8.0 The `$name` parameter was added.
* @since 5.5.0 The `$args` parameter was added.
*
* @param string|null $name Name of the specific header file to use. Null for the default header.
* @param array $args Additional arguments passed to the header template.
*/
do_action( 'get_header', $name, $args );
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "header-{$name}.php";
}
$templates[] = 'header.php';
if ( ! locate_template( $templates, true, true, $args ) ) {
return false;
}
}
Hooks
- do_action( ‘get_header’,
string|null $name ,array $args ) Fires before the header template file is loaded.
Multiple Headers
Different header for different pages.
The file names for the home and 404 headers should be
header-home.php
andheader-404.php
respectively.As second parameter in get_header() we can pass an array
We will be able to use this in
header.php
Named header template
Load an alternate header file by using the
$name
param:<?php get_header( 'special' ); ?>
The above code in a theme file will load the template file:
header-special.php
. If not found, will default to loading:header.php
.Pass array as second parameter in get_header()
Put a code on header.php
Output:
Show menu item that setup in the WordPress dashboard
Note:
In the above code class
wpdocs_header_menu
assign to the menu and also idwpdocs_nav_menu
assign to the menu. So you can style the menu according to the requirements.Simple 404 page
The following code is a simple example of a template for an “HTTP 404: Not Found” error (which you could include in your theme as
404.php
).