注册侧边栏和边栏小工具。不显示侧边栏小部件

时间:2013-03-05 作者:Michael Ecklund

我正在注册边栏和边栏小部件。

该主题目前支持两个侧栏。主要和次要。

add_action(\'widgets_init\', array($this, \'add_sidebars\'), 10, 2);

public function add_sidebars(){
    register_sidebar(array(
        \'name\' => \'Primary Sidebar\',
        \'id\' => \'mbe-sidebar-primary-sidebar\',
        \'description\' => \'\',
        \'class\' => \'mbe-sidebar\',
        \'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</div>\',
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\'
    ));
    register_sidebar(array(
        \'name\' => \'Secondary Sidebar\',
        \'id\' => \'mbe-sidebar-secondary-sidebar\',
        \'description\' => \'\',
        \'class\' => \'mbe-sidebar\',
        \'before_widget\' => \'<div id="%1$s" class="widget %2$s">\',
        \'after_widget\' => \'</div>\',
        \'before_title\' => \'<h3 class="widget-title">\',
        \'after_title\' => \'</h3>\'
    ));
    $this->add_sidebar_widgets();
}
然后,我将所有预设小部件(post类型中的post对象)添加为可用的边栏小部件。

private function add_sidebar_widgets(){
    global $mbe_content;
    $widgets = $mbe_content->get_content(\'archive\', \'mbe-sidebar-widgets\');
    if(!$widgets){
        return;
    }
    foreach($widgets as $widget){
        wp_register_sidebar_widget(
            \'mbe-sidebar-widget-\'.$widget[\'post_name\'],
            $widget[\'post_title\'],
            array($this, \'display_widget\'),
            array(
                \'description\' => \'Sidebar Widget\'
            ),
            \'\'
        );
    }
}

public function display_widget($args, $params){
    echo \'widget\';
}
我还将这些侧栏小部件设置为主侧栏中的活动小部件。例如,在主题级别,如果我只想使用动态侧边栏。。。

if(is_active_sidebar(\'mbe-sidebar-primary-sidebar\')){
    dynamic_sidebar(\'mbe-sidebar-primary-sidebar\');
} else{
    echo \'NO PRIMARY WIDGETS!\'.PHP_EOL;
}
它显示的是我的widget中的“widget”,但这就是我的widget的输出所在。所以这无关紧要。关键是,动态侧栏正在工作,我可以看到我的小部件输出。

我的一个问题是,如果我在侧边栏中添加一个简单的默认WordPress小部件,它怎么不会在侧边栏上输出任何内容?就好像它根本不存在或者什么的。

How can I get my widgets to output on the sidebar, while still allowing the default sidebar widgets provided by WordPress to output on the sidebar as well?

我原本以为是我的function display_widget() 所以我尝试获取所有小部件,循环浏览它们,然后调用the_widget() 在他们的每个小部件ID上,但是我得到了关于找不到小部件的错误,诸如此类。

1 个回复
SO网友:MikeNGarrett

从主题中检查模板文件。如果没有启用其他小部件,可能会手动添加一些小部件。

如果是这种情况,请修改侧栏模板中的条件语句,以便在那里设置的默认值之后输出小部件。

@s\\u ha\\u dum基本上了解了真相。

结束

相关推荐

Sidebar limiting to 10 posts?

我的单曲中有以下代码。php页面:UPDATED CODE <?php if (have_posts()) : the_post(); $post_id = get_the_ID(); if ( is_single() ) { $cats = get_the_category(); $cat = $cats[0]; } else { // category archives