详解WordPress开发中get_header()获取头部函数的用法

函数意义详解
从以后主题调用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');
}

get_header函数的应用

<?php get_header( $name ); ?>

很简略,从下面的函数申明中咱们也能看出,该函数只承受一个变量作为参数。

参数解释
$name ,从下面的函数申明中咱们能够看出,$name是一个字符串型变量,用来调用header的别名模板,
比方 $name = “ab”;
也就是咱们这样

<?php 
  $name = “ab”
  get_header( $name ); 
 
?>

这将会调用 header-ab.php 文件作为头部文件的调用。

例子:

1.简略的 404 页面

上面的代码是一个简略模板文件,专门用来显示 "HTTP 404: Not Found" 谬误的 (这个文件应该蕴含在你的主题中,名为 404.php)

<?php get_header(); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

2.多种头部

为不同的页面显示不同的头部

<?php
if ( is_home() ) :
 get_header( 'home' );
elseif ( is_404() ) :
 get_header( '404' );
else :
 get_header();
endif;
?>

这些为 home 和 404 预备的头部应该辨别命名为  header-home.php 和 header-404.php 。

以上就是安达网络工作室关于《详解WordPress开发中get_header()获取头部函数的用法》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress get_header 主题 PHP编程
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress中"无奈将上传的文件挪动至"谬误的处理办法

明天在网页上传图片到博客,后果提醒:“无奈将上传的文件挪动至 /home/wwwroot/wp-content/uploads/2013/”...

在WordPress的后盾中增加顶级菜单和子菜单的函数详解

增加设置页面-add_menu_page函数 add_menu_page(),这个函数是往后盾增加顶级菜单先,也就是和“外观”、“...

WordPress 完成简略的企业网站

Berita是一个专为此类需要设计的收费theme。较之于同类产品,它同时为了添加定制化水平还革新了wp零碎,应用...

修正wordpress上传暂时目录处理wordpress无奈装置插件包的办法

处理办法这是由于空间中的&rsquo;C:\WINDOWS\TEMP&rsquo;目录没有设置IIS拜访权限,需求空间商为你设置目录...

Wordpress 的用户治理教程

要害字形容:教程 治理 用户 权限 能够 编辑 注册 Wordpress 咱们 后面咱们讲述了 Wordpress 插件的应用。...

WordPress中主动激活插件的完成办法

有一个一劳永逸的办法能够使wordpress博主当前上传插件都不必再手动激活,wordpress主动激活,办法很简略,...

需求提交

客服服务

亿鸽在线客服系统