在 sage 主题中添加自定义小部件时出错

Error while adding custom widget in sage theme

我是 sage 主题的新手。我正在尝试在 lib/setup.php 文件中添加自定义小部件,但出现 Class 'Roots\Sage\Setup\WP_Widget' not found in {path} 错误。

以下是我的代码:

class Banner_Widget extends WP_Widget {
 function __construct() {
    $this->WP_Widget('Banner-Widget', __('Banner Widget', 'blogerist'), $widget_ops);
    add_action('save_post', array(&$this, 'flush_widget_cache'));
    add_action('deleted_post', array(&$this, 'flush_widget_cache'));
    add_action('switch_theme', array(&$this, 'flush_widget_cache'));
}
public function form($instance) {
    //form content

}
function widget($args, $instance) {
    //widget content
}
function update($new_instance, $old_instance) {
    $instance = array_map('strip_tags', $new_instance);
    $this->flush_widget_cache();
    $alloptions = wp_cache_get('alloptions', 'options');
    if (isset($alloptions['Banner-Widget'])) {
        delete_option('Banner-Widget');
    }
    return $instance;
}
function flush_widget_cache() {
    wp_cache_delete('Banner-Widget', 'widget');
}
}

如果您想通过扩展 WP_Widget 创建 class,那么您应该使用全局命名空间。

在WP_Customize_Control前加一个\号。

class Banner_Widget extends \WP_Widget {
  //....
 }