2

这是我的最终目标:隐藏不包含我的主题当前显示为空白的小部件的侧边栏。

背景:我可以通过添加/更改内容和侧边栏 div 的类来更改布局。我唯一遇到的问题是获取特定侧边栏中是否有任何小部件。

研究:我尝试了几种不同的方法,我知道是什么阻止了我正常工作,但我不知道如何解决它。

了解 WordPress 功能:

  • wp_get_sidebars_widgets();- 返回每个侧边栏和活动小部件的数组

我正在尝试使用的插件让我失望:

  • 小部件上下文- 包含每页/帖子显示/隐藏小部件的设置。我推荐它,它按预期工作。我正在尝试向我的网站添加额外的功能,这隐藏了页面上的小部件,但它仍然显示小部件在wp_get_sidebars_widgets();功能中处于活动状态。

小部件上下文插件函数

  • function check_widget_visibility( $widget_id ) { ... }- 传递小部件 ID,如果小部件显示在页面上,则返回 true 或 false。

这是我尝试check_widget_visibility在主题 sidebar.php 文件中调用该函数的内容:

//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()我知道这个函数是在一个类内部调用的,所以我尝试了几种不同的方法来调用名为 Widget Context Plugin 类内部的函数widget_context

  • widget_context()->check_widget_visibility( $widgetID )
    • 退货Fatal error: Call to undefined function widget_context()
  • $widget_context->check_widget_visibility($widgetID)
    • 退货Fatal error: Call to a member function check_widget_visibility() on a non-object

有人可以帮助我了解如何在主题文件中调用函数,该函数位于插件的类中

如果您需要我从 Widget Context Plugin 发布更多代码,请告诉我。

4

1 回答 1

4

因此,经过一些高级谷歌搜索和反复试验,我想通了:

注意:在从插件调用函数之前,最好检查并确保插件已激活。为此,我使用了这个特定的插件

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();
}

接下来,一些插件需要通过执行某个函数来加载。再次对于小部件上下文插件,我需要:

$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
...

我知道有些人不会喜欢这个,因为它是我的插件和主题所独有的,但我知道其他人可以从第一个语句中学习来解决类似的问题。

于 2013-06-16T03:27:26.800 回答