最近更新到PHP8,我得到了一个与主题相关的关键错误。
关键错误是:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php on line 62 and at least 2 expected in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php:163 Stack trace: #0 /www/scottsimonbooks_396/public/wp-includes/class-wp-widget-factory.php(62): WP_Widget->__construct() #1 /www/scottsimonbooks_396/public/wp-includes/widgets.php(115): WP_Widget_Factory->register('thinker_recentp...') #2 /www/scottsimonbooks_396/public/wp-content/themes/thinker/inc/widgets.php(78): register_widget('thinker_recentp...') #3 /www/scottsimonbooks_396/public/wp-content/themes/thinker/functions.php(241): require('/www/scottsimon...') #4 /www/scottsimonbooks_396/public/wp-settings.php(585): include('/www/scottsimon...') #5 /www/scottsimonbooks_396/public/wp-config.php(87): require_once('/www/scottsimon...') #6 /www/scottsimonbooks_396/public/wp-load.php(50): require_once('/www/scottsimon...') #7 /www/scottsimonbooks_396/public/wp-blog-header.php(13): require_once('/www/scottsimon...') #8 /www/scottsimonbooks_396/public/index.php(17): require('/www/scottsimon...') #9 {main} thrown in /www/scottsimonbooks_396/public/wp-includes/class-wp-widget.php on line 163
我尝试通过交换更新第63行中的class-wp-widget-factory.php:
$this->widgets[ $widget ] = new $widget();
对于这个$this->widgets[ $widget ] = new $widget( $widget, $widget );
,但没有任何效果。
(“thinker_recentp……”)是最近发布的一个小部件。
wp-include/class-wp-widget.php中第163行的代码是:
if ( ! empty( $id_base ) ) {
$id_base = strtolower( $id_base );
} else {
$id_base = preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) );
}
$this->id_base = $id_base;
$this->name = $name;
$this->option_name = 'widget_' . $this->id_base;
$this->widget_options = wp_parse_args(
$widget_options,
array(
'classname' => str_replace( '\\', '_', $this->option_name ),
'customize_selective_refresh' => false,
)
);
$this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) );
}```
发布于 2023-02-24 11:50:31
错误指示WP_Widget::__construct()函数至少需要传递两个参数,但在发生错误的代码行中没有传递任何参数。这可能是由于PHP 8的升级,而PHP 8对参数计数的要求更严格。
要解决这个问题,您需要修改WP_Widget类中的“最近的帖子”小部件类中的代码,以便在构造函数函数中包含所需的参数。您需要修改创建WP_Widget类新实例的行,如下所示:
替换此代码:
parent::__construct( 'thinker_recentposts', __( 'Thinker Recent Posts', 'thinker' ), $widget_ops );
使用此代码:
parent::__construct( 'thinker_recentposts', __( 'Thinker Recent Posts', 'thinker' ), array( 'description' => __( 'Displays recent posts with thumbnails', 'thinker' ) ) );
区别在于,我们添加了一个包含"description“参数的数组,该参数是WP_Widget构造函数所需的参数之一。
完成此更改后,保存文件并尝试重新加载。错误信息不应再出现。
https://wordpress.stackexchange.com/questions/414147
复制相似问题