我正在编写一个小部件,用于通过输入图像的本地路径来显示图像。但停留在显示图像的阶段。可能,问题存在于函数中:widget。我尝试了很多方法,(您可以在小部件功能中看到代码),但仍然无法正常工作。有人可以指出我的错误吗?
<?php
class oseBadgeWidget extends WP_Widget{
public function oseBadgeWidget(){
$widget_ops = array(
'classname' => 'ose-badge-widget',
'description' => 'Show the OSE Firewall Badget'
);
$control_ops = array(
'width' => 200, 'height' => 250,
);
$this->WP_Widget('ose_Badge_Widget', 'OSE Badge Widget', $widget_ops, $control_ops);
}
public function __construct(){
parent:: __construct(
'ose_Badge_Widget',
'OSE Badge Widget',
array('description' => __('Show the OSE Firewall Badget'), )
);
}
// show the widget appearence
public function form($instance){
if (isset($instance['file_path'])){ $file_path = $instance[ 'file_path' ]; } else { $image_width = __( '', 'text_domain' ); }
?>
<p>
<label for="<?php echo $this->get_field_id('file_path' ); ?>"><?php _e( 'File Path:' ); ?></label>
<input class="file_path" id="<?php echo $this->get_field_id( 'file_path' ); ?>" name="<?php echo $this->get_field_name( 'file_path' ); ?>" type="text" value="<?php echo esc_attr( $file_path ); ?>" />
</p>
<?php
}
public function update($new_instance, $old_instance){
$instance['file_path'] = strip_tags( $new_instance['file_path'] );
return $instance;
}
// find the image from the input path, and show it
public function widget($args, $instance){
extract($args);
if ($instance['file_path'] != ''){$file_path = $instance['file_path'];} else { $file_path = '';}
$handle = opendir($file_path);
$file = readdir($handle);
echo $before_widget;
echo '<img src= "picture/'.$file.'" border = "0" />';
echo $after_widget;
//$handle = opendir(dirname(realpath(__FILE__)).'/picture/');
//$file = readdir($handle);
//echo '<img src= "picture/'.$file.'" border = "0" />';
}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "oseBadgeWidget" );' ) );
?>