我正在注册边栏和边栏小部件。
该主题目前支持两个侧栏。主要和次要。
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上,但是我得到了关于找不到小部件的错误,诸如此类。