语法
get_header( string $name = null )
参数
$name
(string) (Optional)
描述
参数默认为空,自动加载header.php文件。
如果设置了参数 $name ,则会加载header-{name}.php文件。
示例
简单的404页面
<?php get_header(); ?>
<h2><?php esc_html_e( 'Error 404 - Not Found', 'textdomain' ); ?></h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
根据页面加载不同的头部文件
<?php
if ( is_home() ) :
get_header( 'home' );
elseif ( is_404() ) :
get_header( '404' );
else :
get_header();
endif;
?>
关系
调用
wp-includes/plugin.php: do_action()
wp-includes/template.php: locate_template()
源码
function get_header( $name = null ) {
/**
* Fires before the header template file is loaded.
*
* @since 2.1.0
* @since 2.8.0 $name parameter added.
*
* @param string|null $name Name of the specific header file to use. null for the default header.
*/
do_action( 'get_header', $name );
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "header-{$name}.php";
}
$templates[] = 'header.php';
locate_template( $templates, true );
}