2013-11-24 112 views
1

我已经完成了这个小部件。在管理区域中,当我更改变量号码的值时,不保存小号和大号。而在前台,我有以下声明:注意:wordpress部件中的undefined索引

注意:未定义指数:数/home/masqueci/public_html/wp-content/themes/Flatnews/includes/theme-widgets.php线272

的对于除标题变量之外的所有变量都相同。

这是小工具代码:

/* 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 
    } 

} 

我不知道什么是坏的,我做的其他部件和我haven't有这个问题我想我已经写了相同的代码。任何帮助?

+0

http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php – brasofilo

+0

我知道这个通知的意思,但问题是,变量定义(我认为),如果他们没有定义我不知道问题在哪里 – ThemesCreator

回答

0

您需要在调用变量前为变量赋值。不要总是假定用户会给你所需的全部数据。

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; 
    } 
+0

添加“空”默认值不会使通知消失。如果使用实际的默认值进行编辑,这可能会工作 –

0

将这个在你的函数,像这样的顶级:

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 ... 
} 

显然设置真实的默认值可能是有意义的,但空白的可能至少会删除您收到的警告和通知。

我刚更新了我的小部件插件。它和你的问题一样。下面是使用我的回答而来的例子: http://plugins.svn.wordpress.org/sm-sticky-featured-widget/tags/1.2.5/sticky-featured-widget.php