检查当前前端中是否显示 WordPress 小部件 [英] Checking If A WordPress Widget Displayed In The Current Front End
本文介绍了检查当前前端中是否显示 WordPress 小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有检查小部件是否在当前前端显示的功能?
这是拉取一些样式或脚本或对小部件执行其他操作所必需的.
Is there a function to check if a widget is displayed in the current front end?
This is necessary for pulling some styles or scripts or doing other action to the widget.
推荐答案
请通过回显 $GLOBALS['displayed_sidebars'] 和 $GLOBALS['displayed_widgets'] 的内容来查看这是否有效,例如使用 print_r.
Please see if this works by echoing the contents $GLOBALS['displayed_sidebars'] and $GLOBALS['displayed_widgets'], using print_r for example.
必须在对要包含的所有侧边栏执行 dynamic_sidebar 后对其进行测试.
It must be tested after dynamic_sidebar has been executed for all sidebars that you want to include.
add_filter( 'dynamic_sidebar_params', function( $params ) {
global $displayed_sidebars, $displayed_widgets;
if( !in_array( $params[0]['id'], $displayed_sidebars ))
$displayed_sidebars[] = $params[0]['id'];
if( !in_array( $params[0]['widget_name'], $displayed_widgets ))
$displayed_widgets[] = $params[0]['widget_name'];
return $params;
});
这篇关于检查当前前端中是否显示 WordPress 小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文