这是我的最终目标:隐藏不包含我的主题当前显示为空白的小部件的侧边栏。
背景:我可以通过添加/更改内容和侧边栏div的类来更改布局。我遇到的唯一问题是获取特定侧边栏中是否有任何小部件。
研究:我尝试了几种不同的方法,但我知道是什么阻碍了我正常运作,但我不知道如何解决它。
理解WordPress功能:
wp_get_sidebars_widgets();
- 返回每个插件让我误解了我正在尝试使用的方法:
wp_get_sidebars_widgets();
函数中显示小部件是活动的。小部件上下文插件函数
function check_widget_visibility( $widget_id ) { ... }
- 传递窗口小部件ID,如果窗口小部件显示在页面上,则返回true或false。以下是我尝试在主题sidebar.php文件中调用check_widget_visibility
函数的内容:
//get the sidebars and active widgets and set to a variable
$sidebarActiveWidgets = wp_get_sidebars_widgets();
//set variable that will be set to true if any widgets are active for this page
$activeWidgets = false;
//'sidebar' is the name of the sidebar that I am trying to check for active widgets
foreach($sidebarActiveWidgets['sidebar'] as $widgetID){
//check if the widget is visible
if(check_widget_visibility( $widgetID )){
//widget is visible set variable to true
$activeWidgets = true;
//widget is visible no reason to check others so break the foreach loop
break 1;
}
}
//check if the variable is still false
if(! $activeWidgets){
//variable is false run additional operations to extend content and remove sidebar that contains no active functions
}
我收到错误:Fatal error: Call to undefined function check_widget_visibility()
我知道这个函数是在类内部调用的,所以我尝试了几种不同的方法来调用名为{{1}的Widget Context Plugin类中的函数}:
widget_context
widget_context()->check_widget_visibility( $widgetID )
Fatal error: Call to undefined function widget_context()
有人可以帮我解决如何在主题文件中调用函数的问题,其中函数位于插件的类中
如果您需要我从Widget Context Plugin发布更多代码,请告诉我。
答案 0 :(得分:4)
因此经过一些高级谷歌搜索和反复试验后,我发现了它:
注意:在从插件调用函数之前,最好检查一下make sure the plugin is activated。要使用我使用的特定插件执行此操作
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
...
}
接下来,这个小部件使用已经存在的php公共类,所以为了再次使用它,我需要将这个类设置为这样的变量的对象;
if(class_exists('widget_context')){
$my_widget_context = new widget_context();
}
接下来,某些插件需要通过执行某个功能来加载。对于widget-context插件,我需要:
$my_widget_context->load_plugin_settings();
现在,我可以正确地调用插件的类功能,如:
$my_widget_context->check_widget_visibility( $widgetID )
为了解决这个问题,我已经为项目的这一部分包含了整个代码:
...
//see if context_widget plugin is installed and activated
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
if(is_plugin_active('widget-context/widget-context.php')){
//get the sidebars and active widgets and set to a variable
$sidebarActiveWidgets = wp_get_sidebars_widgets();
//define variable to call context_widget class
if(class_exists('widget_context')){
$my_widget_context = new widget_context();
}
//load plugin settings
$my_widget_context->load_plugin_settings();
//set variable that will be set to true if any widgets are active for this page
$activeWidgets = false;
//loop through each widget that is activated for this sidebar
foreach($sidebarActiveWidgets['sidebar'] as $widgetID){
//check if the widget is visible
if($my_widget_context->check_widget_visibility( $widgetID )){
//widget is visible set variable to true
$activeWidgets = true;
//widget is visible no reason to check others so break the foreach loop
break 1;
}
}
//check if the variable is still false
if(! $activeWidgets){
?>
<script type="text/javascript">
<!--
//javascript here to resize main content
//-->
</script>
<?php
//variable is false run additional operations to extend content and remove sidebar that contains no active functions
}
} else {
//the widget-context plugin is not active so set widgets to active state
$activeWidgets = true;
}
//use and if statement with $activeWidgets to display sidebar or not
...
我知道有些人不喜欢这个,因为它对我的插件和主题来说是独一无二的,但我知道其他人可以从第一个语句中学习来解决类似的问题。