我正在寻找一种解决方案来检查特定页面上的 is_active_widget 。
我得到了这个https://wordpress.stackexchange.com/questions/2302/loading-scripts-only-if-a-pspecial-shortcode-or-widget-is-present
但是 sorich87 的第二种方式仍然加载脚本每个页面上的小部件,其中运行/活动页面上没有特定的活动小部件。即使每个页面的侧边栏有不同的 ID 和名称。
例如:
我有页面“A”(带有侧边栏“A1”),其中包含“X”活动小部件和 x_scripts。然后,当我检查不包含“X”小部件的页面“B”(带有侧边栏“B1”)时,它仍然加载“X”小部件的 x_scripts。看来这个 is_active_widget 函数只检查一般激活的小部件,而不是在运行页面上。
这是我的代码:
class X extends WP_Widget
{
function X(){
$widget_ops = array('description' => 'X widget');
$control_ops = array('width' => 200, 'height' => 500);
parent::WP_Widget(false,$name='X-widget',$widget_ops,$control_ops);
if ( is_active_widget(false, false, $this->id_base, true) ){
add_action('wp_head', 'x_styles',0);
add_action( 'wp_footer', 'x_scripts' );
}
}
//rest of functions here
}
Run Code Online (Sandbox Code Playgroud)
如何防止这种情况发生?