在动态网站中,基本上每个网页都会有几部分变化特别少或者几乎不变的地方,比如导航,比如页脚。在WordPress网站中也是这样,为了方便的得到固定的网页头部,WordPress提供了get_header函数给我们调用头部文件。
函数原型
该函数位于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 ?>包裹,调用方法为:
<?php get_header();?>
函数说明
由上面的函数原型可以看出,get_header函数需要一个字符串类型参数,并且该参数允许为空,为空时查找主题目录下header.php文件,不存在时使用默认的 wp-includes/theme-compat/header.php 作为头部,不为空时查找header-参数名.php文件作为头部。
如传入daimadog
<?php get_header('daimadog');?>
此时将加载主题目录下的header-daimadog.php作为网站的头部文件。
WordPress函数
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。