WordPress at a glance

get_header() WP 1.5.0

Includes the header.php template file from your current theme's directory. If a $name is specified then header-{name}.php file will be included.

If the theme contains no header.php file then the header from the default theme wp-includes/theme-compat/header.php will be included.

Works based on: locate_template()
Hooks from the function
Return

null/false. Void on success, false if the template does not exist.

Usage

get_header( $name, $args );
$name(string)
The name of the specialised header.
Default: null
$args(array)
Additional arguments passed to the header template.
Default: empty array

Examples

#1 Simple 404 page

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

#2 Multiple Headers

<?php
if ( is_home() ) :
	get_header( 'home' );
elseif ( is_404() ) :
	get_header( '404' );
else :
	get_header();
endif;
?>

The file names for the home and 404 headers should be header-home.php and header-404.php respectively.

Changelog

Since 1.5.0 Introduced.
Since 5.5.0 A return value was added.
Since 5.5.0 The $args parameter was added.

Code of get_header() WP 5.5.1

<?php
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;
	}
}

From category: Theme files connection

More from Template Tags: Main Functions

vladlu 100vlad.lu
Editors: kama 100
No comments
    Log In . Register