函数文档

is_header_video_active()

💡 云策文档标注

概述

is_header_video_active() 函数用于检查自定义头部视频是否应在当前页面显示。它基于主题支持和回调函数来确定视频的显示状态,并可通过过滤器进行修改。

关键要点

  • 函数返回布尔值:true 表示应显示自定义头部视频,false 表示不应显示。
  • 首先检查主题是否支持 custom-header 的 video 设置,若不支持则直接返回 false。
  • 如果设置了 video-active-callback 回调函数,则调用该函数决定显示状态;否则默认显示视频(通常基于 is_front_page())。
  • 提供 apply_filters('is_header_video_active', $show_video) 钩子,允许开发者过滤显示逻辑。

代码示例

function is_header_video_active() {
	if ( ! get_theme_support( 'custom-header', 'video' ) ) {
		return false;
	}

	$video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );

	if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) {
		$show_video = true;
	} else {
		$show_video = call_user_func( $video_active_cb );
	}

	/**
	 * Filters whether the custom header video is eligible to show on the current page.
	 *
	 * @since 4.7.0
	 *
	 * @param bool $show_video Whether the custom header video should be shown. Returns the value
	 *                         of the theme setting for the `custom-header`'s `video-active-callback`.
	 *                         If no callback is set, the default value is that of `is_front_page()`.
	 */
	return apply_filters( 'is_header_video_active', $show_video );
}

注意事项

  • 此函数自 WordPress 4.7.0 版本引入。
  • 相关函数包括 get_theme_support() 和 apply_filters(),用于获取主题支持和应用过滤器。
  • 被 the_custom_header_markup() 和 has_custom_header() 等函数使用,影响自定义头部视频的显示。

📄 原文内容

Checks whether the custom header video is eligible to show on the current page.

Return

bool True if the custom header video should be shown. False if not.

Source

function is_header_video_active() {
	if ( ! get_theme_support( 'custom-header', 'video' ) ) {
		return false;
	}

	$video_active_cb = get_theme_support( 'custom-header', 'video-active-callback' );

	if ( empty( $video_active_cb ) || ! is_callable( $video_active_cb ) ) {
		$show_video = true;
	} else {
		$show_video = call_user_func( $video_active_cb );
	}

	/**
	 * Filters whether the custom header video is eligible to show on the current page.
	 *
	 * @since 4.7.0
	 *
	 * @param bool $show_video Whether the custom header video should be shown. Returns the value
	 *                         of the theme setting for the `custom-header`'s `video-active-callback`.
	 *                         If no callback is set, the default value is that of `is_front_page()`.
	 */
	return apply_filters( 'is_header_video_active', $show_video );
}

Hooks

apply_filters( ‘is_header_video_active’, bool $show_video )

Filters whether the custom header video is eligible to show on the current page.

Changelog

Version Description
4.7.0 Introduced.