我在我的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;
}
}
答案 0 :(得分:1)
在函数形式中,$ title,$ category,$ the_limit未明确定义。如果它们是$ instance数组中的键/值条目,则只会定义它们。你最好的选择是var_dump($ instance)来查看你的the_limit是否在那里。