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


当前第2页 返回上一页

函数使用实例
下面的例子来自于 默认主题 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

返回前面的内容

相关阅读 >>

wordpress中登陆后关闭登陆页面及设置用户不可见栏目

wordpress之wp_nav_menu使用说明

在centos 6 中安装 wordpress(二)安装wordpress

wordpress wp_list_categories(分类的链接列表)的使用方法

wordpress数据库的基本构造及各表结构

wordpress忘记后台密码怎么办 轻松找回wp密码的方法

wordpress中给媒体文件添加分类和标签的php功能实现

六个步骤,从零开始教你搭建基于wordpress的个人博客

wordpress的.htaccess优化技巧

详解wordpress中用于合成数组的wp_parse_args()函数

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...