Php 注意:wordpress小部件中的未定义索引
我已经完成了这个小部件。在管理区域中,当我更改变量编号的值时,小的和大的不会被保存。在前端我有一个通知: 注意:未定义索引:第272行/home/masqueci/public_html/wp content/themes/Flatnews/includes/theme-widgets.php中的编号 除title变量外,所有变量都相同 这是小部件代码:Php 注意:wordpress小部件中的未定义索引,php,wordpress,widget,notice,Php,Wordpress,Widget,Notice,我已经完成了这个小部件。在管理区域中,当我更改变量编号的值时,小的和大的不会被保存。在前端我有一个通知: 注意:未定义索引:第272行/home/masqueci/public_html/wp content/themes/Flatnews/includes/theme-widgets.php中的编号 除title变量外,所有变量都相同 这是小部件代码: /* CUSTOM TAG CLOUD */ class Tag_cloud extends WP_Widget { functio
/* CUSTOM TAG CLOUD */
class Tag_cloud extends WP_Widget {
function Tag_cloud() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'Tag_cloud', 'description' => __('Display tag cloud.', 'fabulous') );
/* Widget control settings. */
$control_ops = array( 'width' => 200, 'height' => 350, 'id_base' => 'tag_cloud' );
/* Create the widget. */
$this->WP_Widget( 'tag_cloud', __('Fabulous tag cloud', 'fabulous'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
/* User-selected settings. */
$title = apply_filters('widget_title', $instance['title'] );
$number = $instance['number'];
$small = $instance['small'];
$large = $instance['large'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Title of widget (before and after defined by themes). */
if ( $title )
echo $before_title . '<i class="fa fa-tags"></i>' . $title . $after_title;
echo fab_show_tags ($small, $large, $number);
/* After widget (defined by themes). */
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['number'] = strip_tags( $new_instance['number'] );
$instance['small'] = strip_tags( $new_instance['small'] );
$instance['large'] = strip_tags( $new_instance['large'] );
return $instance;
}
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => 'TAG CLOUD', 'number' => '10', 'small' => '8', 'large' => '12');
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">
<?php _e('Title:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>">
<?php _e('Number:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'small' ); ?>">
<?php _e('Smallest:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'small' ); ?>" name="<?php echo $this->get_field_name( 'small' ); ?>" value="<?php echo $instance['small']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'large' ); ?>">
<?php _e('Largest:', 'fabulous') ?>
</label>
<input id="<?php echo $this->get_field_id( 'large' ); ?>" name="<?php echo $this->get_field_name( 'large' ); ?>" value="<?php echo $instance['large']; ?>" style="width:100%;" />
</p>
<?php
}
}
/*自定义标记云*/
类标记_云扩展WP_小部件{
函数标记_cloud(){
/*小部件设置*/
$widget_ops=array('classname'=>'Tag_cloud','description'=>'Display Tag cloud','fableus');
/*控件设置*/
$control_ops=数组('width'=>200,'height'=>350,'id_base'=>tag_cloud');
/*创建小部件*/
$this->WP_小部件('tag_cloud','fablead tag cloud','fablead'),$Widget_ops,$control_ops);
}
函数小部件($args$instance){
摘录($args);
/*用户选择的设置*/
$title=apply_filters('widget_title',$instance['title']);
$number=$instance['number'];
$small=$instance['small'];
$large=$instance['large'];
/*在widget之前(由主题定义)*/
echo$before_小部件;
/*小部件的标题(在主题定义之前和之后)*/
如果($标题)
回音$before_title.'.$title.$before_title;
echo fab_show_标签($small、$large、$number);
/*在小部件之后(由主题定义)*/
echo$after_小部件;
}
函数更新($new\u实例,$old\u实例){
$instance=$old_实例;
/*剥离标签(如果需要)并更新小部件设置*/
$instance['title']=strip_标记($new_instance['title']);
$instance['number']=strip_标签($new_instance['number']);
$instance['small']=strip_标记($new_instance['small']);
$instance['large']=strip_标记($new_instance['large']);
返回$instance;
}
函数形式($instance){
/*设置一些默认小部件设置*/
$defaults=数组('title'=>'tagcloud','number'=>'10','small'=>'8','large'=>'12');
$instance=wp\u parse\u args((数组)$instance$defaults);
?>
在调用变量之前,您需要为变量分配一个值。不要总是假定用户会提供所需的所有数据
function widget( $args, $instance ) {
extract( $args );
/* User-selected settings. */
$title = null; $number = null; $small = null; $large = null;
if (! empty( $instance['title'] ) ) { $title = apply_filters('widget_title', $instance['title'] ) }
if (! empty( $instance['number'] ) ) { $number = $instance['number']; }
if (! empty( $instance['small'] ) ) { $small = $instance['small']; }
if (! empty( $instance['large'] ) ) { $large = $instance['large']; }
/* Before widget (defined by themes). */
echo $before_widget;
/* Title of widget (before and after defined by themes). */
if ( $title )
echo $before_title . '<i class="fa fa-tags"></i>' . $title . $after_title;
echo fab_show_tags ($small, $large, $number);
/* After widget (defined by themes). */
echo $after_widget;
}
函数小部件($args,$instance){
摘录($args);
/*用户选择的设置*/
$title=null;$number=null;$small=null;$large=null;
如果(!empty($instance['title']){$title=apply_filters('widget_title',$instance['title'])}
如果(!empty($instance['number']){$number=$instance['number'];}
如果(!empty($instance['small']){$small=$instance['small'];}
如果(!empty($instance['large']){$large=$instance['large'];}
/*在widget之前(由主题定义)*/
echo$before_小部件;
/*小部件的标题(在主题定义之前和之后)*/
如果($标题)
回音$before_title.'.$title.$before_title;
echo fab_show_标签($small、$large、$number);
/*在小部件之后(由主题定义)*/
echo$after_小部件;
}
在调用变量之前,您需要为变量分配一个值。不要总是假设用户会给您提供所需的所有数据
function widget( $args, $instance ) {
extract( $args );
/* User-selected settings. */
$title = null; $number = null; $small = null; $large = null;
if (! empty( $instance['title'] ) ) { $title = apply_filters('widget_title', $instance['title'] ) }
if (! empty( $instance['number'] ) ) { $number = $instance['number']; }
if (! empty( $instance['small'] ) ) { $small = $instance['small']; }
if (! empty( $instance['large'] ) ) { $large = $instance['large']; }
/* Before widget (defined by themes). */
echo $before_widget;
/* Title of widget (before and after defined by themes). */
if ( $title )
echo $before_title . '<i class="fa fa-tags"></i>' . $title . $after_title;
echo fab_show_tags ($small, $large, $number);
/* After widget (defined by themes). */
echo $after_widget;
}
函数小部件($args,$instance){
摘录($args);
/*用户选择的设置*/
$title=null;$number=null;$small=null;$large=null;
如果(!empty($instance['title']){$title=apply_filters('widget_title',$instance['title'])}
如果(!empty($instance['number']){$number=$instance['number'];}
如果(!empty($instance['small']){$small=$instance['small'];}
如果(!empty($instance['large']){$large=$instance['large'];}
/*在widget之前(由主题定义)*/
echo$before_小部件;
/*小部件的标题(在主题定义之前和之后)*/
如果($标题)
回音$before_title.'.$title.$before_title;
echo fab_show_标签($small、$large、$number);
/*在小部件之后(由主题定义)*/
echo$after_小部件;
}
将其放在函数顶部,如下所示:
class My_Widget extends WP_Widget {
protected $defaults;
function _construct(){
$this->defaults = array(
'title' => '',
'number' => 5,
'small' => 1,
'large' => 10,
);
}
// etc etc rest of class functions ...
}
然后在表单函数的顶部添加这一行,就像这样:
function form($instance) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
// etc etc, rest of form ...
}
显然,设置真正的默认值是有意义的,但是空的默认值至少可以删除您收到的警告和通知
我刚刚更新了我的小部件插件。它与您的插件有相同的问题。下面是我的答案中使用的示例:
将其放在函数顶部,如下所示:
class My_Widget extends WP_Widget {
protected $defaults;
function _construct(){
$this->defaults = array(
'title' => '',
'number' => 5,
'small' => 1,
'large' => 10,
);
}
// etc etc rest of class functions ...
}
然后在表单函数的顶部添加这一行,就像这样:
function form($instance) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
// etc etc, rest of form ...
}
显然,设置真正的默认值是有意义的,但是空的默认值至少可以删除您收到的警告和通知
我刚刚更新了我的小部件插件。它与您的插件有相同的问题。下面是我的答案中使用的示例:
我知道这个通知的意思,但问题是变量被定义了(我想)如果它们没有定义我不知道问题在哪里我知道这个通知的意思,但问题是变量被定义了(我想)如果它们没有定义我不知道问题在哪里加上“null”默认值不会使通知消失。如果使用实际默认值编辑,这可能会起作用。添加“null”默认值不会使通知消失。如果使用实际默认值编辑,这可能会起作用