您可以使用该功能is_active_widget . 例如。:
function check_widget() {
if( is_active_widget( \'\', \'\', \'search\' ) ) { // check if search widget is used
wp_enqueue_script(\'my-script\');
}
}
add_action( \'init\', \'check_widget\' );
要在仅加载小部件的页面中加载脚本,必须在小部件类中添加is\\u active\\u widget()代码。E、 例如,请参阅默认的最近评论小部件(wp includes/default-widgets.php,第602行):
class WP_Widget_Recent_Comments extends WP_Widget {
function WP_Widget_Recent_Comments() {
$widget_ops = array(\'classname\' => \'widget_recent_comments\', \'description\' => __( \'The most recent comments\' ) );
$this->WP_Widget(\'recent-comments\', __(\'Recent Comments\'), $widget_ops);
$this->alt_option_name = \'widget_recent_comments\';
if ( is_active_widget(false, false, $this->id_base) )
add_action( \'wp_head\', array(&$this, \'recent_comments_style\') );
add_action( \'comment_post\', array(&$this, \'flush_widget_cache\') );
add_action( \'transition_comment_status\', array(&$this, \'flush_widget_cache\') );
}