the_custom_header_markup()
云策文档标注
概述
the_custom_header_markup() 函数用于输出自定义页眉的 HTML 标记,适用于 WordPress 主题开发。在 Customizer 预览中,始终会输出一个容器 div。
关键要点
- 函数调用 get_custom_header_markup() 获取自定义页眉标记,若为空则直接返回。
- 如果页眉视频处于活动状态且已设置或处于 Customizer 预览中,会通过 wp_enqueue_script() 和 wp_localize_script() 加载并本地化 'wp-custom-header' 脚本。
- 相关函数包括 get_custom_header_markup()、is_header_video_active()、has_header_video()、get_header_video_settings()、is_customize_preview()、wp_enqueue_script() 和 wp_localize_script()。
代码示例
function the_custom_header_markup() {
$custom_header = get_custom_header_markup();
if ( empty( $custom_header ) ) {
return;
}
echo $custom_header;
if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) {
wp_enqueue_script( 'wp-custom-header' );
wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() );
}
}注意事项
- 该函数从 WordPress 4.7.0 版本开始引入。
- 在 Customizer 预览中,即使没有自定义页眉,也会输出一个容器 div,以确保预览效果一致。
原文内容
Prints the markup for a custom header.
Description
A container div will always be printed in the Customizer preview.
Source
function the_custom_header_markup() {
$custom_header = get_custom_header_markup();
if ( empty( $custom_header ) ) {
return;
}
echo $custom_header;
if ( is_header_video_active() && ( has_header_video() || is_customize_preview() ) ) {
wp_enqueue_script( 'wp-custom-header' );
wp_localize_script( 'wp-custom-header', '_wpCustomHeaderSettings', get_header_video_settings() );
}
}
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |