0

我在我的 php 日志中收到以下未定义的变量错误,但它在下面的函数中明确定义。难道我做错了什么?

 [18-Sep-2013 15:17:23 UTC] PHP Notice:  Undefined variable: the_limit in /Volumes/Mac HD/Sites/thesite/wp-content/themes/site2.7/functions.php on line 313

 class EditorsPicks extends WP_Widget{
    function __construct()
    {
        $params=array(
            'description' => 'Editors Picks',
            'name'        => 'Editors Picks'            );

        parent::__construct('EditorsPicks','',$params);
    }

    public function form($instance)
    {
        extract($instance);

        // Title
        echo '<p><label for="'.$this->get_field_id('title').'">Title:</label>';
        echo '<input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'"';
        echo 'value="';
        echo !empty($title) ? esc_attr($title) : 'Editor\'s Picks';
        echo '"/></p>';

        // Category
        echo '<p><label for="'.$this->get_field_id('category').'">Category ID:</label>';
        echo '<input class="widefat" type="number" id="'.$this->get_field_id('category').'" name="'.$this->get_field_name('category').'" value="';
        echo !empty($category) ? esc_attr($category) : 75646;
        echo '"/></p>';

        // Limit
        echo '<p><label for="'.$this->get_field_id('the_limit').'">Limit:</label>';
        echo '<input class="widefat" type="number" id="'.$this->get_field_id('the_limit').'" name="'.$this->get_field_name('the_limit').'" value="';
        echo !empty($the_limit) ? esc_attr($the_limit) : 5;
        echo '"/></p>';
    }

    function widget($args,$instance){
        extract($args);
        extract($instance);

        echo $before_widget;
        echo $before_title.$title.$after_title;

        global $post;
        $editors_picks=get_posts(array('cat'=>$category,'posts_per_page'=>$the_limit));

        echo '<ul class="editors_picks">';
        foreach($editors_picks as $post) : setup_postdata($post);
            $image = wp_get_attachment_image_src(get_post_thumbnail_id(),array(236,190));

            echo '<li class="clickable">';
            echo '<a rel="nofollow" href="'.get_permalink().'" title="'.get_the_title().'"><img width="100" height="81" src="'.$image[0].'" alt="'.get_the_title().'"/></a>';
            echo '<div class="title"><a href="'.get_permalink().'" title="'.get_the_title().'">'.textLimit(get_the_title(),80).'</a></div>';
            echo '</li>';
        endforeach;
        echo '</ul>';
        echo $after_widget;
    }
 }
4

1 回答 1

1

在函数形式中,$title、$category、$the_limit 没有明确定义。只有当它们是 $instance 数组中的键/值条目时,它们才会被定义。你最好的选择是 var_dump($instance) 看看你的 the_limit 是否在那里。

于 2013-09-18T17:23:57.583 回答