问题描述
我有这个警告:
PHP Warning: sprintf(): Too few arguments in /var/www/wordpress/wp-includes/widgets.PHP on line 1193
function the_widget( $widget,$instance = array(),$args = array() ) {
global $wp_widget_factory;
if ( ! isset( $wp_widget_factory->widgets[ $widget ] ) ) {
_doing_it_wrong(
__FUNCTION__,sprintf(
/* translators: %s: register_widget() */
__( 'Widgets need to be registered using %s,before they can be displayed.' ),'<code>register_widget()</code>'
),'4.9.0'
);
return;
}
$widget_obj = $wp_widget_factory->widgets[ $widget ];
if ( ! ( $widget_obj instanceof WP_Widget ) ) {
return;
}
$default_args = array(
'before_widget' => '<div class="widget %s">','after_widget' => '</div>','before_title' => '<h2 class="widgettitle">','after_title' => '</h2>',);
$args = wp_parse_args( $args,$default_args );
$args['before_widget'] = sprintf( $args['before_widget'],$widget_obj->widget_options['classname'] );
$instance = wp_parse_args( $instance );
/** This filter is documented in wp-includes/class-wp-widget.PHP */
$instance = apply_filters( 'widget_display_callback',$instance,$widget_obj,$args );
if ( false === $instance ) {
return;
}
widget.PHP 的第 1193 行是:
$args['before_widget'] = sprintf( $args['before_widget'],$widget_obj->widget_options['classname'] );
我不知道如何解决它,我怕做错了什么,因为我是 PHP 新手,而且该函数不是我创建或修改的
解决方法
我自己通过在行中添加一个 id 来解决我的问题:
$args['before_widget'] = sprintf( $args['before_widget'],$widget_obj->id,$widget_obj->widget_options['classname'] );
我不知道为什么或在哪里,但 $args['before_widget'] 返回:
'<section id="%1$s" class="widget %2$s">'
所以它需要 2 个参数,所以我只添加 $widget_obj->id 并且我没有任何错误消息! 我希望有一天它会帮助某人:)