我正在创建一个具有以下功能的小部件:用户可以向他们添加图像,它们可以显示在前端。我有admin.php文件,当我创建输入类型文件表单时。 在my.widget.php中是主要的小部件文件。在这个功能中:
public function widget( $args, $instance ) {
if ( ! isset ( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
extract( $args, EXTR_SKIP );
$widget_string = $before_widget;
$title = isset( $instance[ 'my-file' ] ) ? $instance[ 'my-file' ] : '';
}
我尝试保存输入类型文件“my-file”,但点击“保存”按钮后,它不会保存。我也试过$_FILES[ 'my-file' ]
,但仍然没有保存。
注意:如果我将输入类型更改为“text”,则保存,但是使用file - not。
问题是:我如何在窗口小部件中保存输入类型文件?
答案 0 :(得分:0)
以下是创建窗口小部件的完整代码,并在数据库中保存字段。
在function.php文件中添加代码
require get_template_directory().'/classes/widget.php';
$widgets = new wpb_widget();
在我们在上面的代码中包含的widget.php文件中添加以下代码。
//创建小部件
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('Services Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on Services Widget', 'wpb_widget_domain' ), )
);
}
//创建小部件前端
//这是行动发生的地方
public function widget( $args, $instance ) {
global $wpdb;
$title = apply_filters( 'widget_title', $instance['title'] );
$numberofpost = apply_filters( 'widget_title', $instance['numberofpost'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo '<div class="glossymenu"><div class="acor">';
echo $args['before_title'];
echo '<h2 class="menuitem submenuheader"><span class="glossyspan">'.$title.'</span></h2>';;
echo '<div style="text-align:right">
<a href="" class="prevprog"><img src="'.get_template_directory_uri().'/images/inner_prev.png" alt="inner_prev"></a>
<a href="" class="nextprog"><img src="'.get_template_directory_uri().'/images/inner_next.png" alt="inner_next"></a>
</div>';
echo $args['after_title'];
$query = new WP_Query(array('post_type' => 'services','posts_per_page'=>$numberofpost,'orderby'=> 'ID','order' => 'DESC'));
$totalrecord = $query->post_count;
echo '<div id="programs">';
$mm = 0;
while ($query->have_posts()) : $query->the_post();
$thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(127, 123) );
if($mm == 0)
{
echo '<div class="submenu"><ul>';
}
$expert = strip_tags(get_the_excerpt());
if (strlen($expert) > 56) {
// truncate string
$stringCut = substr($expert, 0, 56);
// make sure it ends in a word so assassinate doesn't become ass...
$expert = substr($stringCut, 0, strrpos($stringCut, ' '));
}
echo '<li>
<img width="58" src="'.$thumb_image_url[0].'" alt="'.$totalrecord.'" />
<span class="bText">'.get_the_title().'</span>
<span class="sText">'.$expert.'</span></li>';
$mm++;
if($mm == 4 or ($totalrecord == $mm))
{
echo '</ul></div>';
$mm = 0;
}
endwhile; wp_reset_query();
echo '</div>';
echo '</div></div>';
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'Services', 'wpb_widget_domain' );
}
if ( isset( $instance[ 'numberofpost' ] ) ) {
$numberofpost = $instance[ 'numberofpost' ];
}
else {
$numberofpost = __( '5', 'wpb_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'numberofpost' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'numberofpost' ); ?>" name="<?php echo $this->get_field_name( 'numberofpost' ); ?>" type="text" value="<?php echo esc_attr( $numberofpost ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['numberofpost'] = ( ! empty( $new_instance['numberofpost'] ) ) ? strip_tags( $new_instance['numberofpost'] ) : '';
return $instance;
}
}
//类wpb_widget在这里结束