《PHP應用:WordPress主題制作中自定義頭部的相關PHP函數解析》要點:
本文介紹了PHP應用:WordPress主題制作中自定義頭部的相關PHP函數解析,希望對您有用。如果有疑問,可以聯系我們。
PHP應用header_image()
header_image() 函數是 WordPress 自定頂部圖像的尺度接口函數,該函數可以自動判斷后臺設置,并返回字符串形式的用戶自定義頂部圖像地址.本文主要涉及該函數的詳解及使用.
PHP利用【Display header image path.】 即,顯示頂部圖像地址.
使用
PHP利用<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
PHP應用
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 );
}
PHP應用get_custom_header 自定義頂部
get_custom_header 函數是 WordPress 3.4 送給我們的新禮物,該函數的出現是為了更好的集成和封裝頂部的使用,本文主要對 get_custom_header 這個函數進行詳解、以及如安在 WordPress 3.4 版本的主題中集成頂部功能.
PHP應用請注意,依據本文折騰你的主題時,請確保你的 WordPress 已經升級到 3.4版本.
PHP應用get_custom_header 意義詳解
自定義頂部目前大部分主題主要用到的還只是兩個功能 1.自定義頂部圖像 2.自定義頂部樣式
具體的效果你可以看一下 默認主題 twenty eleven ,或者我的另一個博客 悠悠我心
本函數是 WP 3.4 版本后才出現的一個內置函數,主要用于將用戶設置的頂部的各項參數以對象(object)的形式返回.
單單說這么句屁話,也許你還不明白,想要明白的話,請往下看.
請注意本函數與get_header()有著本色的區別.
PHP利用函數使用實例
下面的例子來自于 默認主題 twenty eleven 中 header.php 文件
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;
}
PHP應用綜合使用詳解
以下主要援引官方文檔解釋 自定義頂部
PHP應用
//打開主題自界說頂部支持
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 );
PHP應用自界說頂部圖像
PHP應用
//打開主題自界說頂部支持
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 );
PHP應用自適應頂部圖像設置
PHP應用
$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 );
PHP應用自定義頂部圖像的挪用
PHP利用
<img
src="<?php header_image(); ?>"
height="<?php echo get_custom_header()->height; ?>"
width="<?php echo get_custom_header()->width; ?>"
alt=""
/>
《PHP應用:WordPress主題制作中自定義頭部的相關PHP函數解析》是否對您有啟發,歡迎查看更多與《PHP應用:WordPress主題制作中自定義頭部的相關PHP函數解析》相關教程,學精學透。維易PHP學院為您提供精彩教程。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/7763.html