php getHeader(,get_header()

在 WordPress 的主题函数中,为了简化我们写诸如include这种包含文件的写法,特别为我们准备了 get_header()、get_footer()、get_sidebar()等主题模板文件包含语句。今天就备忘一下get_header()这个函数吧。

get_header函数,是我们接触 WordPress 后,最先接触的几个函数之一,到目前为止,我接触到的大部分主题,都需要用到这个函数(废话)。

函数意义详解

从当前主题调用header.php文件。是不是很简单?好吧,如果你是新手的话这里要提醒一下,这里的get和get_children()、get_category中的get略有不同之处。

get_header函数声明(定义)

之前写文章很少会写到函数定义的代码,后来自己翻看的时候发现这个习惯不太好,所以决定,只要篇幅允许,就会把函数主题贴出来,方便自己翻看。

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');

}

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');

}

get_header函数的使用

很简单,从上面的函数声明中我们也能看出,该函数只接受一个变量作为参数。

参数解释

$name ,从上面的函数声明中我们可以看出,$name是一个字符串型变量,用来调用header的别名模板,

比如 $name = “sb”;

也就是我们这样

$name = “sb”

get_header( $name );

?>

$name = “sb”

get_header( $name );

?>

那我我们将会调用 header-sb.php 文件作为头部文件的调用。

懂了?

总结

结合诸如get_template_part()和get_header(本函数)这种别名机制,其实我们可以轻而易举的创建出庞大的,花样繁多的、别致的……主题。后面的就是折腾了不是吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值