Using WordPress ‘get_header()’ PHP function

The get_header() WordPress PHP function loads the header template for a theme. If a name is specified, a specialized header will be included.

Usage

get_header( $name, $args )

Input:

get_header( 'special', array( 'title' => 'My Custom Title' ) );

Output:
Loads the header-special.php file with a custom title.

Parameters

  • $name (string, optional): The name of the specialized header. Default: null
  • $args (array, optional): Additional arguments passed to the header template. Default: array()

More information

See WordPress Developer Resources: get_header()

Examples

Load different headers for different pages

Load different header templates based on the current page:

if ( is_home() ) {
    get_header( 'home' );
} elseif ( is_404() ) {
    get_header( '404' );
} else {
    get_header();
}

Pass additional arguments to the header

Pass an array of additional arguments to the header template:

get_header( '', array( 'name' => 'John Doe', 'age' => 30 ) );

In header.php:

<p>Hey, <?php echo $args['name']; ?>, you are <?php echo $args['age']; ?> years old.</p>

Load a named header template

Load an alternate header file by using the $name parameter:

get_header( 'special' );

Pass a menu as an argument to the header

Pass a menu array as an argument to the header template:

get_header( '', array( 'menu' => wp_nav_menu( array(
    'menu' => 'primary_menu',
    'menu_class' => 'header_menu',
    'menu_id' => 'nav_menu',
) ) ) );

In header.php:

<?php echo $args['menu']; ?>

Simple 404 page

A simple example of a 404 error page template:

get_header();
?>
<h2><?php esc_html_e( 'Error 404 - Not Found', 'textdomain' ); ?></h2>
<?php
get_sidebar();
get_footer();