我有一个自定义小部件,我已经做了,但每当我改变下拉值,它总是恢复到第一个选项,当我保存小部件。如何让它显示用户选择的任何选项?
// update widget
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance[\'category\'] = strip_tags($new_instance[\'category\']);
return $instance;
}
// widget form creation
function form($instance) {
// Check values
if( $instance) {
$category = esc_attr($instance[\'app_category\']);
$instance[\'category\'] = strip_tags( $new_instance[\'category\'] );
} else {
$category = \'\';
}
//Set up some default widget settings.
$defaults = array( \'category\' => \'\');
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<!-- Category Select Menu -->
<p>
<label for="<?php echo $this->get_field_id(\'category\'); ?>"><?php _e(\'App Category: \', \'AppManager_plugin\'); ?></label>
<h1><?php echo $category ?></h1>
<h1><?php echo $instance[\'category\'] ?></h1>
<select id="<?php echo $this->get_field_id(\'category\'); ?>" name="<?php echo $this->get_field_name(\'Category\'); ?>" >
<?php foreach(get_terms(\'app_category\',\'parent=0&hide_empty=0\') as $term) { ?>
<option <?php selected( $instance[\'category\'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
</p>