官網文檔,如何實現WordPress主題頭部圖像自定義的。
add_theme_support()
在 functions.php
文件中,可以自定義頭部的一些背景顏色,圖像等等, 例如:
add_theme_support( 'custom-header' );
這樣便會在WP後台-外觀-出現-頂部菜單,從而進行頭部圖像自定義設置。
參數使用
$defaults = array( 'default-image' => '', 'width' => 0, 'height' => 0, 'flex-height' => false, 'flex-width' => false, 'uploads' => true, 'random-default' => false, 'header-text' => true, 'default-text-color' => '', 'wp-head-callback' => '', 'admin-head-callback' => '', 'admin-preview-callback' => '', ); add_theme_support( 'custom-header', $defaults );
實例
1、設置一個自定義頭形象
設定默認頭部圖片 980px width, 60px height:
$args = array( 'width' => 980, 'height' => 60, 'default-image' => get_template_directory_uri() . '/images/header.jpg', ); add_theme_support( 'custom-header', $args ); Upload other custom header images
2、設置一個默認標題形象和允許網站所有者上傳其他圖片:
$args = array( 'width' => 980, 'height' => 60, 'default-image' => get_template_directory_uri() . '/images/header.jpg', 'uploads' => true, ); add_theme_support( 'custom-header', $args );
3、靈活頭部設置
$args = array( 'flex-width' => true, 'width' => 980, 'flex-height' => true, 'height' => 200, 'default-image' => get_template_directory_uri() . '/images/header.jpg', ); add_theme_support( 'custom-header', $args );
頭部文件 header.php調用
<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />