新版本的 WordPress 自定义头部功能和自定义背景很像,自 3.4 版本后,为主题添加自定义头部功能方法很简单那,只需要往主题的 functions.php 中添加代码:
- 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 );
实例:设置一个默认的头部图像,
- $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 );
后台的设置都做好了,前台调用设置选项的方法:
- <img src=“<?php header_image(); ?>” height=“<?php echo get_custom_header()->height; ?>” width=“<?php echo get_custom_header()->width; ?>” alt=“” />