国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress主題制作中自定義頭部的相關PHP函數解析

2024-09-07 00:48:05
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了WordPress主題制作中自定義頭部的相關PHP函數解析,包括header_image()函數和get_custom_header()函數的用法講解,需要的朋友可以參考下

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 ); 
  20. 自定義頂部圖像 
  21. //打開主題自定義頂部支持 
  22. add_theme_support( 'custom-header' ); 
  23.  
  24. $headarg = array(//將設置打包成數組 
  25.  'default-image'     => ''
  26.  'random-default'     => false, 
  27.  'width'         => 0, 
  28.  'height'         => 0, 
  29.  'flex-height'      => false, 
  30.  'flex-width'       => false, 
  31.  'default-text-color'   => ''
  32.  'header-text'      => true, 
  33.  'uploads'        => true, 
  34.  'wp-head-callback'    => ''
  35.  'admin-head-callback'  => ''
  36.  'admin-preview-callback' => ''
  37. ); 
  38. //將數組中的設置添加到自定義頂部上 
  39. add_theme_support( 'custom-header'$headarg ); 
  40. 自適應頂部圖像設置 
  41. $args = array
  42.  'flex-width'  => true,//自適應高度 
  43.  'width'     => 980, 
  44.  'flex-width'  => true,//自適應寬度 
  45.  'height'    => 200, 
  46.  'default-image' => get_template_directory_uri() . '/images/header.jpg'
  47. ); 
  48. 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. />

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜陵市| 平邑县| 罗山县| 咸丰县| 阳信县| 营山县| 安图县| 藁城市| 桐庐县| 新野县| 上林县| 柳江县| 武夷山市| 云浮市| 拜泉县| 汉沽区| 洞头县| 敦煌市| 南川市| 五常市| 沙雅县| 沾化县| 贵阳市| 朝阳县| 增城市| 灵丘县| 荆州市| 林芝县| 沙田区| 手机| 霍邱县| 临武县| 茂名市| 定边县| 昆明市| 荆门市| 泸州市| 翼城县| 乐亭县| 临漳县| 阳泉市|