微件选择选项未保存

时间:2013-10-15 作者:user40497

我做了这个小部件,但所选选项在更新时不会保存。代码如下:

/* CUSTOM POSTS LIST */
class Posts_list extends WP_Widget {

    function Posts_list() {
        /* Widget settings. */
        $widget_ops = array( \'classname\' => \'posts_list\', \'description\' => __(\'Display differents posts list.\', \'fabulous\') );
        /* Widget control settings. */
        $control_ops = array( \'width\' => 200, \'height\' => 350, \'id_base\' => \'posts_list\' );
        /* Create the widget. */
        $this->WP_Widget( \'posts_list\', __(\'Fabulous posts list\', \'fabulous\'), $widget_ops, $control_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );
        /* User-selected settings. */
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $id = $instance[\'id\'];
        $type = $instance[\'type\'];
        $num = $instance[\'num\'];
        $img = $instance[\'img\'];
        $meta = $instance[\'meta\'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Title of widget (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title; ?>

        <div id="<?php echo $id ?>">
        <?php if ( $type == \'Recent posts\' ) {
            echo fab_recent_posts($num, $img, $meta);
        } elseif ($type == \'Popular posts\') {
            echo fab_popular_posts($num, $img, $meta);
        } elseif ($type == \'Top rating posts\') {
            echo fab_top_rating($num, $img, $meta);
        } ?>
        </div>
        <?php 

        /* After widget (defined by themes). */
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        /* Strip tags (if needed) and update the widget settings. */
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'id\'] = $new_instance[\'id\'];
        $instance[\'type\'] = $new_instance[\'type\'];
        $instance[\'num\'] = $new_instance[\'num\'];
        $instance[\'img\'] =  isset( $new_instance[\'img\'] );
        $instance[\'meta\'] =  isset( $new_instance[\'meta\'] );
        return $instance;
    }

    function form( $instance ) {
        /* Set up some default widget settings. */
        $defaults = array( \'title\' => \'\', \'id\' =>\'posts-list-widget\', \'type\' => \'recents-posts\', \'num\' => \'5\', \'img\' => 1, \'meta\' => 1);    
        $instance = wp_parse_args( (array) $instance, $defaults ); 
        $type = isset( $new_instance[\'type\'] );
        ?>
<p>
<label for="<?php echo $this->get_field_id( \'title\' ); ?>">
<?php _e(\'Title:\', \'fabulous\') ?>
</label>
<input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id( \'id\' ); ?>">
<?php _e(\'Name:\', \'fabulous\') ?>
</label>
<input id="<?php echo $this->get_field_id( \'id\' ); ?>" name="<?php echo $this->get_field_name( \'id\' ); ?>" value="<?php echo $instance[\'id\']; ?>" style="width:100%;" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'type\'); ?>">
<?php _e(\'Choose posts list type:\', \'fabulous\'); ?>
</label>
<select name="<?php echo $this->get_field_name(\'type\'); ?>" id="<?php echo $this->get_field_id(\'type\'); ?>" class="widefat">
<?php
$types = array (\'Recent posts\', \'Top rating posts\', \'Popular posts\');
foreach ($types as $option) {
echo \'<option value="\' . $option . \'" id="\' . $option . \'"\', $type == $option ? \' selected="selected"\' : \'\', \'>\', $option, \'</option>\';
}?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( \'num\' ); ?>">
<?php _e(\'Number of posts:\', \'fabulous\') ?>
</label>
<input id="<?php echo $this->get_field_id( \'num\' ); ?>" name="<?php echo $this->get_field_name( \'num\' ); ?>" value="<?php echo $instance[\'num\']; ?>" style="width:100%;" />
</p>
<p>
  <input class="checkbox" type="checkbox" <?php checked(isset( $instance[\'img\']) ? $instance[\'img\'] : 0  ); ?> id="<?php echo $this->get_field_id( \'showrecents\' ); ?>" name="<?php echo $this->get_field_name( \'img\' ); ?>" />
  <label for="<?php echo $this->get_field_id( \'img\' ); ?>">
<?php _e(\'Display image\', \'fabulous\') ?>
</label>
</p>
<p>
  <input class="checkbox" type="checkbox" <?php checked(isset( $instance[\'meta\']) ? $instance[\'meta\'] : 0  ); ?> id="<?php echo $this->get_field_id( \'meta\' ); ?>" name="<?php echo $this->get_field_name( \'meta\' ); ?>" />
  <label for="<?php echo $this->get_field_id( \'meta\' ); ?>">
<?php _e(\'Display meta\', \'fabulous\') ?>
</label>
</p>
<?php
    }

}
没人能帮我吗?谢谢

1 个回复
SO网友:s_ha_dum

此行$type = isset( $instance[\'type\'] ); 将设置$type 至任一truefalse 它与您稍后检查的字符串从不匹配:

foreach ($types as $option) {
   echo \'<option value="\' . $option . \'" id="\' . $option . \'"\', $type == $option ? \' selected="selected"\' : \'\', \'>\', $option, \'</option>\';
}
你需要删除它$type = isset( $instance[\'type\'] ); 线路和使用$instance[\'type\']option.

您还应该考虑使用selected().

结束

相关推荐

MySQL select query with ajax

我使用ajax将变量传递给列表。php在我的212模板中。在列表中。php我正在执行mysql select查询,但当我在控制台中看到时,我收到以下错误:<br /> <b>Fatal error</b>: Call to a member function get_results() on a non-object in <b>D:\\xampp\\htdocs\\wordpress\\wp-content\\themes\\twentytwel