详解WordPress开发中get_header()获取头部函数的用法,wordpressheader
函数意义详解
从当前主题调用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 id="Error-Not-Found">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主题制作之模板文件的引入方法
- WordPress的主题编写中获取头部模板和底部模板
- WordPress主题中添加文章列表页页码导航的PHP代码实例
- WordPress中使主题支持小工具以及添加插件启用函数
- 实现WordPress主题侧边栏切换功能的PHP脚本详解
- 编写PHP脚本使WordPress的主题支持Widget侧边栏
- 基于wordpress主题制作的具体实现步骤
- wordpress主题支持自定义菜单及修改css样式实现方法

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Atom編輯器mac版下載
最受歡迎的的開源編輯器

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境