wordpress get_header()函数介绍

2020-08-30 11:49:54

【函数介绍】

wordpress使用的是PHP语言的开源系统,我们都知道PHP经常会使用到include来引用PHP文件,wordpress为了简化这些常用文件的引用,特别为我们准备了 get_header()、get_footer()、get_sidebar()等主题模板文件包含语句。get_header()函数是我们主题开发过程必用的函数,一般用于包含公用的头部文件header.php,如果你有多个头部文件你可以使用get_header($name)来获取指定头部文件header-{$name}.php。

【函数使用】



【参数说明】

$name
(string) (可选) 获取指定头部文件,格式为header-name.php.
默认: None

【参数实例】

404页面设置:

Error 404 - Not Found

多个头部,获取主题中的头部文件header-test.php:


$name = 'test';


【源文件】

get_header()位于wp-includes/general-template.php,函数代码如下:


function get_header( $name = null ) {
do_action( 'get_header', $name );

$templates = array();
if ( isset($name) )
$templates[] = "header-{$name}.php";

$templates[] = 'header.php';

// Backward compat code will be removed in a future release
if ('' == locate_template($templates, true))
load_template( ABSPATH . WPINC . '/theme-compat/header.php');
}