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

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="" 
/>

以上就是安达网络工作室关于《WordPress主题制作中自定义头部的相关PHP函数解析》的一些看法。更多内容请查看本栏目更多内容!

本文相关话题: WordPress 主题 头部 PHP编程
版权声明:本文为 安达网络工作室 转载文章,如有侵权请联系我们及时删除。
相关文章
WordPress分类与标签等存档页完成置顶的办法

本文实例讲述了WordPress分类与标签等存档页完成置顶的办法。分享给大家供大家参考。详细剖析如下: 在word...

Wordpress(Wp)3.5版修正默许上传图片门路的处理办法

置信接触过WP零碎的冤家都理解,老版零碎的上传图片门路是默许指向wp-content目录下的。做过WP博客的冤家应...

WordPress数据备份心得分享

效劳器钱用光了要关了或许是效劳器想要搬家,需求备份各种数据。 明天简略的备份了一下在效劳器下面wordpre...

Crayon Syntaxer 弱小的WP代码高亮插件装置办法

有意中发现一款性能弱小的Wordpress代码高亮插件 &ndash; Crayon Syntaxer,我以为能够算是目前款式最多、易...

WordPress后盾显示相干用户文章相干联评论的办法

本文实例讲述了WordPress后盾显示相干用户文章相干联评论的办法。分享给大家供大家参考。详细剖析如下: 将...

wordpress模板罕用函数集锦

根本模板文件 style.css : CSS(款式表)文件index.php : 主页模板archive.php : Archive/Category模板404.ph...

需求提交

客服服务