函数文档

register_default_headers()

💡 云策文档标注

概述

register_default_headers() 函数用于向 WordPress 自定义标题管理界面注册一组默认的标题图像。开发者需提供包含 URL、缩略图 URL 和描述的数组参数,以扩展或覆盖现有注册的标题。

关键要点

  • 函数用于注册自定义标题图像,以便在管理界面中使用。
  • 参数 $headers 是必需的数组,键为字符串 ID,值包含 'url'、'thumbnail_url' 和 'description' 键。
  • 此函数将新标题与已注册的标题合并,存储在全局变量 $_wp_default_headers 中。
  • 使用前必须先注册标题图像,否则无法在自定义标题功能中应用。
  • 自 WordPress 3.0.0 版本引入此函数。

代码示例

register_default_headers( array(
    'wheel' => array(
        'url'           => '%s/images/headers/wheel.jpg',
        'thumbnail_url' => '%s/images/headers/wheel-thumbnail.jpg',
        'description'   => __( 'Wheel', 'twentyeleven' )
    ),
    'shore' => array(
        'url'           => '%s/images/headers/shore.jpg',
        'thumbnail_url' => '%s/images/headers/shore-thumbnail.jpg',
        'description'   => __( 'Shore', 'twentyeleven' )
    ),
    'trolley' => array(
        'url'           => '%s/images/headers/trolley.jpg',
        'thumbnail_url' => '%s/images/headers/trolley-thumbnail.jpg',
        'description'   => __( 'Trolley', 'twentyeleven' )
    )
) );

注意事项

  • 代码示例来自 TwentyEleven 主题文件,需将图像放置在主题目录中,%s 会被替换为主题路径。
  • 在子主题中引用图像时,应使用 %2$s 替代 %s 以指向样式表目录。

📄 原文内容

Registers a selection of default headers to be displayed by the custom header admin UI.

Parameters

$headersarrayrequired
Array of headers keyed by a string ID. The IDs point to arrays containing 'url', 'thumbnail_url', and 'description' keys.

More Information

  • The parameter $headers is required to merge with already registered headers.
  • This function is needed in order to use custom headers. The header image(s) must first be registered before using it.

Source

function register_default_headers( $headers ) {
	global $_wp_default_headers;

	$_wp_default_headers = array_merge( (array) $_wp_default_headers, (array) $headers );
}

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example

    Edit the file functions.php inside your theme and add the following code, replacing the images and descriptions where required.
    The %s is replaced with your themes path, so place the images in your theme directory.

    register_default_headers( array(
    	'wheel' => array(
    		'url'           => '%s/images/headers/wheel.jpg',
    		'thumbnail_url' => '%s/images/headers/wheel-thumbnail.jpg',
    		'description'   => __( 'Wheel', 'twentyeleven' )
    	),
    	'shore' => array(
    		'url'           => '%s/images/headers/shore.jpg',
    		'thumbnail_url' => '%s/images/headers/shore-thumbnail.jpg',
    		'description'   => __( 'Shore', 'twentyeleven' )
    	),
    	'trolley' => array(
    		'url'           => '%s/images/headers/trolley.jpg',
    		'thumbnail_url' => '%s/images/headers/trolley-thumbnail.jpg',
    		'description'   => __( 'Trolley', 'twentyeleven' )
    	)
    ) );

    Note: The code above was lifted from the TwentyEleven theme files.

    To reference a image in a child theme (ie in the stylesheet directory), use %2$s instead of %s.

    register_default_headers( array(
    	'child' => array(
    		'url'           => '%2$s/images/headers/child.jpg',
    		'thumbnail_url' => '%2$s/images/headers/child-thumbnail.jpg',
    		'description'   => __( 'Child', 'twentyeleven' )
    	)
    	)
    ) );