如何在Wordpress中扩展自定义窗口小部件? [英] How to extend a custom widget in wordpress?
问题描述
我正在谈论制作扩展WP_Widget的子小部件的小部件.
I am talking about making a widget which extends a child widget of WP_Widget.
以此作为参考: https://wordpress.stackexchange. com/questions/101438/how-to-extend-a-wp-widget-twice
示例:
My_WidgetBase 扩展了 WP_Widget
My_Widget 扩展了 My_WidgetBase
我想知道如何让 My_Widget 与其他小部件一起显示(当前不是).我已经 My_WidgetBase 开始工作.
I want to know how to get My_Widget to show up with my other widgets (it is not currently). I have gotten My_WidgetBase to work.
这对我的框架很重要.我知道必须重写widget(),update()和form(),但是我在任何地方都找不到关于制作WP_Widget的孙子的任何信息.
This is important for my framework. I understand that widget(), update(), and form() must be overridden, but I could not find anything about making a grandchild of WP_Widget anywhere.
示例:
class My_WidgetBase extends WP_Widget {
public function __construct($id = 'my-widget-base', $desc = 'My WidgetBase', $opts = array()) {
$widget_ops = array();
parent::__construct( $id, $desc, $widget_ops );
}
public function widget( $args, $instance ) {
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
}
public function update( $new_instance, $old_instance ) {
return $new_instance;
}
public function form( $instance ) {
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
return 'noform';
}
}
function RegisterMyWidgets() {
register_widget('My_WidgetBase');
}
add_action( 'widgets_init', 'RegisterMyWidgets' );
推荐答案
Solved digging further into this thread: https://wordpress.stackexchange.com/questions/101438/how-to-extend-a-wp-widget-twice
问题不在于类的实现,而在于如何将父类包括在框架中.我不是在同一个php文件中同时使用子级和子级,而是使用了两个具有不同目录的单独文件.
The problem was not with my implementation of the classes, but how I included the parent in my framework. I was not working with both child and grandchild in the same php file, but two separate files with different directories.
解决此问题是在我的孙子类目录中正确使用require_once()的问题.
Solving this problem was a matter of using require_once() correctly in the directory of my grandchild class.
这篇关于如何在Wordpress中扩展自定义窗口小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!