重要:本文最后更新于2018-11-13 13:36:34,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗。
在动态网站中,基本上每个网页都会有几部分变化特别少或者几乎不变的地方,比如导航,比如页脚。在WordPress网站中也是这样,为了方便的得到固定的网页头部,WordPress提供了get_header函数给我们调用头部文件。
WordPress教程
函数原型
该函数位于wp-include/general-template.php 文件的第 24 – 36 行左右的位置,代码如下。
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');
}
使用方法
PHP函数需要使用<?php ?>包裹,调用方法为:
函数说明
由上面的函数原型可以看出,get_header函数需要一个字符串类型参数,并且该参数允许为空,为空时查找主题目录下header.php文件,不存在时使用默认的 wp-includes/theme-compat/header.php 作为头部,不为空时查找header-参数名.php文件作为头部。
如传入daimadog
此时将加载主题目录下的header-daimadog.php作为网站的头部文件。