頂部影像包括頂部大banner圖片和小的logo圖,自定義頂部是個 2.1 版本中新引入的主題特性. 自定義頂部是在主題的頂部標題部分顯示一個被選擇的影像.
1、添加主題支持
從 3.4 版本 開始, 主題必須在 functions.php 檔案里使用 add_theme_support() 函式來添加自定義頂部的支持, 像這樣:
add_theme_support( 'custom-header' );
添加的默認引數串列:
$defaults = 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', $defaults );
2、范例
設定自定義頂部影像
設定一個默認頂部影像(寬980px和高60px):
$args = array(
'width' => 980,
'height' => 60,
'default-image' => get_template_directory_uri() . '/images/header.jpg',
);
add_theme_support( 'custom-header', $args );
上傳其他自定義頂部影像
設定一個默認頂部影像,并允許網站所有者上傳其他圖片:
$args = array(
'width' => 980,
'height' => 60,
'default-image' => get_template_directory_uri() . '/images/header.jpg',
'uploads' => true,
);
add_theme_support( 'custom-header', $args );
靈活定義你的主題頭部
$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 );
3、前臺呼叫
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />
PS:主題可以在模板中通過get_header_image判斷是否有頂部影像,通過header_image獲得影像地址:
<?php if ( get_header_image() ) : ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<img src="<?php header_image(); ?>" class="header-image" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
</a>
<?php endif; ?>
您可能感興趣的文章:
? wordpress主題制作index.php基本框架代碼
? Wordpress使用Redis快取加速|511遇見強烈推薦
? wordpress判斷文章中是否有圖片
? 第一課:511遇見wordpress本地環境搭建以及多站點配置
? wordpress使用register_post_type 函式創建自定義文章型別∶
? WordPress主題制作教程常用的函式呼叫舉例
? 511遇見網路采用阿里云ECS Wordpress
? wordpress讓你的網頁顯示不同的標題
? 第三課511遇見WordPress主題開發教程基本檔案的建立
? 利用Bootstrap構建你的回應式Wordpress主題(二)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/7665.html
標籤:其他
