第一次使用自定义小部件。我试着这样做:
<?php
namespace App;
class My_Widget extends \\WP_Widget {
function My_Widget() {
$widget_ops = [
\'name\' => \'My widget\',
\'description\' => \'My description\'
];
parent::__construct( \'My_Widget\', \'My widget\', $widget_ops );
}
}
function load_widget() {
register_widget( \'App\\\\my_widget\' );
}
add_action( \'widgets_init\', __NAMESPACE__ . \'\\\\load_widget\' );
我得到这个错误:
Too few arguments to function WP_Widget::__construct(), 0 passed in /srv/www/contraindicaciones.net/current/web/wp/wp-includes/class-wp-widget-factory.php on line 61 and at least 2 expected
我做错了什么?谢谢
最合适的回答,由SO网友:Jacob Peattie 整理而成
您正在使用一种非常过时的创建小部件的方法。您应该使用__construct()
函数,而不是命名函数,作为构造函数,as documented.
namespace App;
class My_Widget extends \\WP_Widget {
function __construct() {
$widget_ops = [
\'name\' => \'My widget\',
\'description\' => \'My description\'
];
parent::__construct( \'My_Widget\', \'My widget\', $widget_ops );
}
}
function load_widget() {
register_widget( \'App\\\\my_widget\' );
}
add_action( \'widgets_init\', __NAMESPACE__ . \'\\\\load_widget\' );