WordPress主题制作中自定义头部的相关PHP函数解析


Posted in PHP onJanuary 08, 2016

header_image()
header_image() 函数是 WordPress 自定顶部图像的标准接口函数,该函数可以自动判断后台设置,并返回字符串形式的用户自定义顶部图像地址。本文主要涉及该函数的详解及使用。

【Display header image path.】 即,显示顶部图像地址。
使用

<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
函数声明源代码
function header_textcolor() {
 echo get_header_textcolor();
}
function get_header_image() {
 $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );
 
 if ( 'remove-header' == $url )
 return false;
 
 if ( is_random_header_image() )
 $url = get_random_header_image();
 
 if ( is_ssl() )
 $url = str_replace( 'http://', 'https://', $url );
 else
 $url = str_replace( 'https://', 'http://', $url );
 
 return esc_url_raw( $url );
}

get_custom_header 自定义顶部
get_custom_header 函数是 WordPress 3.4 送给我们的新礼物,该函数的出现是为了更好的集成和封装顶部的使用,本文主要对 get_custom_header 这个函数进行详解、以及如何在 WordPress 3.4 版本的主题中集成顶部功能。

请注意,根据本文折腾你的主题时,请确保你的 WordPress 已经升级到 3.4版本。

get_custom_header 意义详解
自定义顶部目前大部分主题主要用到的还只是两个功能 1.自定义顶部图像 2.自定义顶部样式
具体的效果你可以看一下 默认主题 twenty eleven ,或者我的另一个博客 悠悠我心
本函数是 WP 3.4 版本后才出现的一个内置函数,主要用于将用户设置的顶部的各项参数以对象(object)的形式返回。
单单说这么句屁话,也许你还不明白,想要明白的话,请往下看。
请注意本函数与get_header()有着本质的区别。

函数使用实例
下面的例子来自于 默认主题 twenty eleven 中 header.php 文件
PHP 代码:

//判断是否存在该函数,以便兼容老版本
if ( function_exists( 'get_custom_header' ) ) {
//get_custom_header()->width 调用带向 width 属性
$header_image_width = get_custom_header()->width;
//get_custom_header()->height 调用带向 height 属性
$header_image_height = get_custom_header()->height;
} else {//兼容老版本的代码
$header_image_width = HEADER_IMAGE_WIDTH;
$header_image_height = HEADER_IMAGE_HEIGHT;
}

综合使用详解
以下主要援引官方文档解释 自定义顶部

//打开主题自定义顶部支持
add_theme_support( 'custom-header' );
 
$headarg = array(//将设置打包成数组
 'default-image'     => '',
 'random-default'     => false,
 'width'         => 0,
 'height'         => 0,
 'flex-height'      => false,
 'flex-width'       => false,
 'default-text-color'   => '',
 'header-text'      => true,
 'uploads'        => true,
 'wp-head-callback'    => '',
 'admin-head-callback'  => '',
 'admin-preview-callback' => '',
);
//将数组中的设置添加到自定义顶部上
add_theme_support( 'custom-header', $headarg );

自定义顶部图像

//打开主题自定义顶部支持
add_theme_support( 'custom-header' );
 
$headarg = array(//将设置打包成数组
 'default-image'     => '',
 'random-default'     => false,
 'width'         => 0,
 'height'         => 0,
 'flex-height'      => false,
 'flex-width'       => false,
 'default-text-color'   => '',
 'header-text'      => true,
 'uploads'        => true,
 'wp-head-callback'    => '',
 'admin-head-callback'  => '',
 'admin-preview-callback' => '',
);
//将数组中的设置添加到自定义顶部上
add_theme_support( 'custom-header', $headarg );

自适应顶部图像设置

$args = array(
 'flex-width'  => true,//自适应高度
 'width'     => 980,
 'flex-width'  => true,//自适应宽度
 'height'    => 200,
 'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );

自定义顶部图像的调用

<img 
  src="<?php header_image(); ?>" 
  height="<?php echo get_custom_header()->height; ?>" 
  width="<?php echo get_custom_header()->width; ?>" 
  alt="" 
/>
PHP 相关文章推荐
ThinkPHP php 框架学习笔记
Oct 30 PHP
PHP中数组的三种排序方法分享
May 07 PHP
使用PHP会话(Session)实现用户登陆功能
Jun 29 PHP
PHP输出日历表代码实例
Mar 27 PHP
PHP实现的随机IP函数【国内IP段】
Jul 20 PHP
Laravel如何使用数据库事务及捕获事务失败后的异常详解
Oct 23 PHP
php框架CodeIgniter使用redis的方法分析
Apr 13 PHP
php中的钩子理解及应用实例分析
Aug 30 PHP
解决laravel(5.5)访问public报错的问题
Oct 12 PHP
解决php用mysql方式连接数据库出现Deprecated报错问题
Dec 25 PHP
PHP文件操作简单介绍及函数汇总
Dec 11 PHP
open_basedir restriction in effect. 原因与解决方法
Mar 14 PHP
微信开发之网页授权获取用户信息(二)
Jan 08 #PHP
详解WordPress开发中get_header()获取头部函数的用法
Jan 08 #PHP
PHP文件操作之获取目录下文件与计算相对路径的方法
Jan 08 #PHP
关于扩展 Laravel 默认 Session 中间件导致的 Session 写入失效问题分析
Jan 08 #PHP
在PHP站点的页面上添加Facebook评论插件的实例教程
Jan 08 #PHP
理解PHP中的Session及对Session有效期的控制
Jan 08 #PHP
PHP实现搜索地理位置及计算两点地理位置间距离的实例
Jan 08 #PHP
You might like
通过对服务器端特性的配置加强php的安全
2006/10/09 PHP
php smarty模版引擎中的缓存应用
2009/12/02 PHP
PHP读取网页文件内容的实现代码(fopen,curl等)
2011/06/23 PHP
将二维数组转为一维数组的2种方法
2014/05/26 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
2014/06/12 PHP
PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
2014/06/23 PHP
PHP依赖倒置(Dependency Injection)代码实例
2014/10/11 PHP
PHP编程入门的基本语法知识点总结
2016/01/26 PHP
最新版本PHP 7 vs HHVM 多角度比较
2016/02/14 PHP
php-fpm开启状态统计的方法详解
2017/06/23 PHP
laravel-admin 在列表页添加自定义按钮的例子
2019/09/30 PHP
Javascript var变量隐式声明方法
2009/10/19 Javascript
niceTitle 基于jquery的超链接提示插件
2010/05/31 Javascript
JQuery Dialog的内存泄露问题解决方法
2010/06/18 Javascript
JQuery开发的数独游戏代码
2010/10/29 Javascript
nodejs分页类代码分享
2014/06/17 NodeJs
jquery实现的Banner广告收缩效果代码
2015/09/02 Javascript
JavaScript提高性能知识点汇总
2016/01/15 Javascript
jQuery Mobile 和 Kendo UI 的比较
2016/05/05 Javascript
angularJs中datatable实现代码
2017/06/03 Javascript
解决vue2.x中数据渲染以及vuex缓存的问题
2017/07/13 Javascript
Form表单上传文件(type=&quot;file&quot;)的使用
2017/08/03 Javascript
67 个节约开发时间的前端开发者的工具、库和资源
2017/09/12 Javascript
node+koa实现数据mock接口的方法
2017/09/20 Javascript
Vue中使用方法、计算属性或观察者的方法实例详解
2018/10/31 Javascript
ES6知识点整理之模块化的应用详解
2019/04/15 Javascript
详解Python3的TFTP文件传输
2018/06/26 Python
python使用隐式循环快速求和的实现示例
2020/09/11 Python
Python 按比例获取样本数据或执行任务的实现代码
2020/12/03 Python
PyTorch 中的傅里叶卷积实现示例
2020/12/11 Python
python爬虫用request库处理cookie的实例讲解
2021/02/20 Python
Wolford法国官网:奥地利奢侈内衣品牌
2020/08/11 全球购物
JavaScript获取当前url根目录(路径)
2014/02/19 面试题
《山谷中的谜底》教学反思
2014/04/26 职场文书
好的旅游活动方案
2014/08/19 职场文书
刚学完怎么用Python实现定时任务,转头就跑去撩妹!
2021/06/05 Python