is_active_widget()检查是否小工具/侧边栏已经在前端显示

目录

描述

This Conditional Tag checks whether widget is displayed on the front-end.

To be effective this function has to run after widgets have initialized, at action 'init' or later.

is_active_widget() 描述

用法

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php is_active_widget( $callback, $widget_id, $id_base, $skip_inactive ); ?>

is_active_widget() 用法

参数

$callback

(string) (可选) Widget callback to check.

默认值: False

$widget_id

(int) (可选) Widget ID. Needed for checking.

默认值: None

$id_base

(string) (可选) Base ID of a widget created by extending WP_Widget.

默认值: None

$skip_inactive

(boolean) (可选) Whether to check in 'wp_inactive_widgets'.

默认值: True

is_active_widget() 参数

返回值

(mixed) 

Returns false if the specified widget is not active, or the id of the sidebar in which the widget is active. If the callback is non-unique you can optionally specify the ID of the widget.

is_active_widget() 返回值

示例

Only load a script when the widget is active

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php
if ( is_active_widget( false, false, $this->id_base, true ) ) {
	wp_enqueue_script( 'jquery' );
}
?>

is_active_widget() 示例

历史

添加于 版本: 2.2.0

is_active_widget() 历史

源文件

is_active_widget() 函数的代码位于 wp-includes/widgets.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Whether widget is displayed on the front-end.
 *
 * Either $callback or $id_base can be used
 * $id_base is the first argument when extending WP_Widget class
 * Without the optional $widget_id parameter, returns the ID of the first sidebar
 * in which the first instance of the widget with the given callback or $id_base is found.
 * With the $widget_id parameter, returns the ID of the sidebar where
 * the widget with that callback/$id_base AND that ID is found.
 *
 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
 * this function has to run after widgets have initialized, at action 'init' or later.
 *
 * @since 2.2.0
 *
 * @global array $wp_registered_widgets
 *
 * @param string $callback      Optional, Widget callback to check.
 * @param int    $widget_id     Optional, but needed for checking. Widget ID.
 * @param string $id_base       Optional, the base ID of a widget created by extending WP_Widget.
 * @param bool   $skip_inactive Optional, whether to check in 'wp_inactive_widgets'.
 * @return string|false False if widget is not active or id of sidebar in which the widget is active.
 */
function is_active_widget($callback = false, $widget_id = false, $id_base = false, $skip_inactive = true) {
	global $wp_registered_widgets;

	$sidebars_widgets = wp_get_sidebars_widgets();

	if ( is_array($sidebars_widgets) ) {
		foreach ( $sidebars_widgets as $sidebar => $widgets ) {
			if ( $skip_inactive && ( 'wp_inactive_widgets' === $sidebar || 'orphaned_widgets' === substr( $sidebar, 0, 16 ) ) ) {
				continue;
			}

			if ( is_array($widgets) ) {
				foreach ( $widgets as $widget ) {
					if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && _get_widget_id_base($widget) == $id_base ) ) {
						if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
							return $sidebar;
					}
				}
			}
		}
	}
	return false;
}

is_active_widget() 源文件

相关