wordpress get_header()函数介绍


【函数介绍】

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

【函数使用】

  1. <?php get_header( $name ); ?>

【参数说明】

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

【参数实例】

404页面设置:

  1. <?php get_header(); ?>
  2. <h2>Error 404 - Not Found</h2>
  3. <?php get_sidebar(); ?>
  4. <?php get_footer(); ?>

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

  1. $name = 'test';
  2. <?php get_header('test'); ?>

【源文件】

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

  1. function get_header( $name = null ) {
  2. do_action( 'get_header', $name );
  3. $templates = array();
  4. if ( isset($name) )
  5. $templates[] = "header-{$name}.php";
  6. $templates[] = 'header.php';
  7. // Backward compat code will be removed in a future release
  8. if ('' == locate_template($templates, true))
  9. load_template( ABSPATH . WPINC . '/theme-compat/header.php');
  10. }