wordpress使用的是PHP语言的开源系统,我们都知道PHP经常会使用到include来引用PHP文件,wordpress为了简化这些常用文件的引用,特别为我们准备了 get_header()、get_footer()、get_sidebar()等主题模板文件包含语句。get_header()函数是我们主题开发过程必用的函数,一般用于包含公用的头部文件header.php,如果你有多个头部文件你可以使用get_header($name)来获取指定头部文件header-{$name}.php。
- <?php get_header( $name ); ?>
$name
(string) (可选) 获取指定头部文件,格式为header-name.php.
默认: None
404页面设置:
- <?php get_header(); ?>
- <h2>Error 404 - Not Found</h2>
- <?php get_sidebar(); ?>
- <?php get_footer(); ?>
多个头部,获取主题中的头部文件header-test.php:
- $name = 'test';
- <?php get_header('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');
- }