帮我解决这个未定义的索引吗?

时间:2011-08-09 作者:MANnDAaR

我正在开发flickr Wp小部件。下面是获取flickr图片的代码。

class imperia_flickr extends WP_Widget {

    function imperia_flickr() {
        $widget_ops = array(\'description\' => \'This Flickr widget populates photos from a Flickr ID.\' );

        parent::WP_Widget(false, __(\'imperia - Flickr\', \'imperiathemes\'),$widget_ops);      
    }

    function widget($args, $instance) {  
        extract( $args );
        $id = $instance[\'id\'];
        $number = $instance[\'number\'];

        echo $before_widget;
        echo $before_title; ?>
        <div class="flickr-icon"></div>
        <?php _e(\'Photostream\'); ?>
            <?php echo $after_title; ?>
            <script type="text/javascript">
            $(document).ready(function() {
            $(\'#flickr\').jflickrfeed({
            limit: <?php echo $number; ?>,
            qstrings: {
            id: \'<?php echo $id; ?>\'
          },
            itemTemplate: \'<li><a href="{{image_b}}" rel="prettyPhoto[pp_gal]"><img class="flickr" src="{{image_s}}" alt="{{title}}" width="45" height="45"></a></li>\'
          }, function(data) {
          $(\'#flickr a\').prettyPhoto();
          });   
            }); 
            </script>        
            <ul id="flickr"></ul>
            <div class="clear"></div>
       <?php            
       echo $after_widget;
   }

   function update($new_instance, $old_instance) {                
       return $new_instance;
   }

   function form($instance) {        
    ******  $id = esc_attr($instance[\'id\']);
    ******  $number = esc_attr($instance[\'number\']);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id(\'id\'); ?>"><?php _e(\'Flickr ID (<a href="http://www.idgettr.com">idGettr</a>):\',\'imperiathemes\'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name(\'id\'); ?>" value="<?php echo $id; ?>" class="widefat" id="<?php echo $this->get_field_id(\'id\'); ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id(\'number\'); ?>"><?php _e(\'Number:\',\'imperiathemes\'); ?></label>
            <select name="<?php echo $this->get_field_name(\'number\'); ?>" class="widefat" id="<?php echo $this->get_field_id(\'number\'); ?>">
                <?php for ( $i = 1; $i <= 12; $i += 1) { ?>
                <option value="<?php echo $i; ?>" <?php if($number == $i){ echo "selected=\'selected\'";} ?>><?php echo $i; ?></option>
                <?php } ?>
            </select>
        </p>
        <?php
    }
} 

register_widget(\'imperia_flickr\');
此小部件工作正常,但出现以下错误

-- Debug: Undefined index: id on line 54 of /wp-content/themes/imperia/includes/widgets/widget-imperia-flickr.php
-- Debug: Undefined index: number on line 55 of /wp-content/themes/imperia/includes/widgets/widget-imperia-flickr.php
我已经用** 在上面

我错在哪里?需要修复什么?

谢谢

1 个回复
最合适的回答,由SO网友:Mamaduka 整理而成

您应该使用isset

$id = isset( $instance[\'id\'] ) ? esc_attr( $instance[\'id\'] ) : \'\';
$number = isset( $instance[\'number\'] ) ? esc_attr( $instance[\'number\'] ) : \'\';

结束

相关推荐

Why use widgets?

我对使用WordPress很陌生,我想知道使用小部件的好处是什么?看here 这听起来像是为那些不是程序员的人准备的,他们想在他们的网站上添加插件。对吗?或者小部件是否在某种程度上使站点更加健壮?