致力于为用户提供真实的
主机测评数据及优惠信息

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

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

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

  1. <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" /> 

函数声明源代码

  1. function header_textcolor() { 

  2.  echo get_header_textcolor(); 

  3. function get_header_image() { 

  4.  $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header''default-image' ) ); 

  5.   

  6.  if ( 'remove-header' == $url ) 

  7.  return false

  8.   

  9.  if ( is_random_header_image() ) 

  10.  $url = get_random_header_image(); 

  11.   

  12.  if ( is_ssl() ) 

  13.  $url = str_replace( 'http://''https://', $url ); 

  14.  else 

  15.  $url = str_replace( 'https://''http://', $url ); 

  16.   

  17.  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 代码:

  1. //判断是否存在该函数,以便兼容老版本 

  2. if ( function_exists( 'get_custom_header' ) ) { 

  3. //get_custom_header()->width 调用带向 width 属性 

  4. $header_image_width = get_custom_header()->width; 

  5. //get_custom_header()->height 调用带向 height 属性 

  6. $header_image_height = get_custom_header()->height; 

  7. else {//兼容老版本的代码 

  8. $header_image_width = HEADER_IMAGE_WIDTH; 

  9. $header_image_height = HEADER_IMAGE_HEIGHT; 

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

  1. //打开主题自定义顶部支持 

  2. add_theme_support( 'custom-header' ); 

  3.   

  4. $headarg = array(//将设置打包成数组 

  5.  'default-image'     => ''

  6.  'random-default'     => false

  7.  'width'         => 0, 

  8.  'height'         => 0, 

  9.  'flex-height'      => false

  10.  'flex-width'       => false

  11.  'default-text-color'   => ''

  12.  'header-text'      => true

  13.  'uploads'        => true

  14.  'wp-head-callback'    => ''

  15.  'admin-head-callback'  => ''

  16.  'admin-preview-callback' => ''

  17. ); 

  18. //将数组中的设置添加到自定义顶部上 

  19. add_theme_support( 'custom-header', $headarg ); 

自定义顶部图像

  1. //打开主题自定义顶部支持 

  2. add_theme_support( 'custom-header' ); 

  3.   

  4. $headarg = array(//将设置打包成数组 

  5.  'default-image'     => ''

  6.  'random-default'     => false

  7.  'width'         => 0, 

  8.  'height'         => 0, 

  9.  'flex-height'      => false

  10.  'flex-width'       => false

  11.  'default-text-color'   => ''

  12.  'header-text'      => true

  13.  'uploads'        => true

  14.  'wp-head-callback'    => ''

  15.  'admin-head-callback'  => ''

  16.  'admin-preview-callback' => ''

  17. ); 

  18. //将数组中的设置添加到自定义顶部上 

  19. add_theme_support( 'custom-header', $headarg ); 

自适应顶部图像设置

  1. $args = array( 

  2.  'flex-width'  => true,//自适应高度 

  3.  'width'     => 980, 

  4.  'flex-width'  => true,//自适应宽度 

  5.  'height'    => 200, 

  6.  'default-image' => get_template_directory_uri() . '/images/header.jpg'

  7. ); 

  8. add_theme_support( 'custom-header', $args ); 

自定义顶部图像的调用

  1. <img  

  2.   src="<?php header_image(); ?>"  

  3.   height="<?php echo get_custom_header()->height; ?>"  

  4.   width="<?php echo get_custom_header()->width; ?>"  

  5.   alt=""  

  6. /> 

赞(0) 打赏
未经允许不得转载:爱主机 » WordPress主题制作中自定义头部的相关PHP函数解析
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址