Я пишу виджет для показа изображения через ввод локального пути изображения. Но застрял на этапе показа изображения. Вероятно, проблема существует в функции: 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");'));
?>
Что именно проблема? p.s, если вы просто хотите показать изображение в виджетах, почему бы вам не поместить его в html в виде текстового/html-виджета в wordpress? –