WordPress函数:get_header
一、函数简介
从当前主题中引入 header.php 模板文件。如果名字是特定的,那么包含特定名称的头部文件 header-{name}.php 就会被引入。
如果主题没有 header.php 文件,就会引入默认文件 wp-includes/theme-compat/header.php 。
<?php get_header($name); ?>
二、函数参数
- $name(string) 调用header-name.php 默认:None
三、函数案例
(一)生成header.php
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<link href="/favicon.ico" rel="shortcut icon" />
<title><?php wp_title('|', true, 'right'); ?></title>
<?php wp_head(); ?>
</head>
<div id="header">
</div>
(二)调用header.php
下面的代码是一个简单模板文件,专门用来显示 "HTTP 404: Not Found" 错误的 (这个文件应该包含在你的主题中,名为 404.php)
<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
四、调试效果
五、源代码
/**
* Loads header template.
*
* 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".
*
* @since 1.5.0
* @since 5.5.0 A return value was added.
* @since 5.5.0 The `$args` parameter was added.
*
* @param string|null $name The name of the specialized header. Default null.
* @param array $args Optional. Additional arguments passed to the header template.
* Default empty array.
* @return void|false Void on success, false if the template does not exist.
*/
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;
}
}
THE END