Wordpress小部件<select>在保存时不显示</select>

时间:2014-01-15 14:04:15

标签: php wordpress widget

我正在尝试创建一个自定义Wordpress Widget,它有一个下拉选择和文本输入字段。一切正常,但是当我回到小部件(例如编辑)时,选择下拉列表总是显示最高结果而不是我的选择。

// New Text and Icon Widget
class Image_Picker extends WP_Widget {

    /** constructor -- name this the same as the class above */
    function Image_Picker() {
    parent::WP_Widget(false, $name = 'Homepage Column Text and Icon Select');   
  } 

  function form($instance) {
      //WIDGET BACK-END SETTINGS
          $message  = $instance['message'];
          $posttype = $instance['posttype'];
         // $posttype = esc_attr($instance['posttype']);  ?>

      <br /><br />

      <label>Choose an icon:</label>
      <select id="<?php echo $this->get_field_id('posttype'); ?>" name="<?php echo $this->get_field_name('posttype'); ?>" class="widefat" style="width:100%;"> 


        <option <?php selected( $instance['posttype'], 'Briefcase Icon'); ?> value="briefcase">Briefcase Icon</option> 
        <option <?php selected( $instance['posttype'], 'Chat Icon'); ?> value="chat">Chat Icon</option> 
        <option <?php selected( $instance['posttype'], 'Document Icon'); ?> value="document">Document Icon</option> 
        <option <?php selected( $instance['posttype'], 'Firstaid Icon'); ?> value="firstaid">Firstaid Icon</option> 
        <option <?php selected( $instance['posttype'], 'Form Icon'); ?> value="form">Form Icon</option> 
        <option <?php selected( $instance['posttype'], 'Graph Icon'); ?> value="graph">Graph Icon</option> 
        <option <?php selected( $instance['posttype'], 'Healthcare Icon'); ?> value="healthcare">Healthcare Icon</option>
        <option <?php selected( $instance['posttype'], 'Heart Icon'); ?> value="heart">Heart Icon</option> 
        <option <?php selected( $instance['posttype'], 'Image Icon'); ?> value="image">Image Icon</option>
        <option <?php selected( $instance['posttype'], 'Leaves Icon'); ?> value="leaves">Leaves Icon</option> 
        <option <?php selected( $instance['posttype'], 'Mail Icon'); ?> value="mail">Mail Icon</option>  
        <option <?php selected( $instance['posttype'], 'Mobile Icon'); ?> value="mobile">Mobile Icon</option> 
        <option <?php selected( $instance['posttype'], 'People Icon'); ?> value="people">People Icon</option>
        <option <?php selected( $instance['posttype'], 'Screen Icon'); ?> value="screen">Screen Icon</option>
        <option <?php selected( $instance['posttype'], 'Stats icon'); ?> value="stats">Stats icon</option>  
        <option <?php selected( $instance['posttype'], 'Video Icon'); ?> value="video">Video Icon</option> 
    </select>

      <br /><br />
      <label for="<?php echo $this->get_field_id('message'); ?>"><?php _e('Text:'); ?></label> 
          <input class="widefat" id="<?php echo $this->get_field_id('message'); ?>" name="<?php echo $this->get_field_name('message'); ?>" type="text" value="<?php echo $message; ?>" />

          <br /><br />

      <?php
      } 

  function update($new_instance, $old_instance) {
      global $posttypes;
      $instance['message'] = strip_tags($new_instance['message']);
      $instance['posttype'] = $new_instance['posttype'];
      return $instance;
  }


  function widget($args, $instance) {
      extract( $args );
      $message  = $instance['message'];
      $posttype = $instance['posttype']; 

        echo ' <li class="item_content clearfix"><a class="features_image" href="#" title="">
        <img src="';
        echo bloginfo('template_url');
        echo '/assets/images/features_large/';
        echo $posttype;
        echo '.png" alt="" />
        </a> <div class="text">';
        echo '' . $message . '<div class="item_footer clearfix">
            <a title="Read more" href="about.html" class="more">Read more &rarr;</a>
        </div></div></li>'; 

  }
}

// Add class for Random Posts Widget
add_action( 'widgets_init', create_function('', 'return register_widget("Image_Picker");') );

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

你没有在selected()函数中使用过值,你需要使用值,如下面的代码所示

<option <?php selected( $instance['posttype'], 'briefcase'); ?> value="briefcase">Briefcase Icon</option> 

同样也改变其他选项的值。它在我的wordpress安装上工作正常!

答案 1 :(得分:-1)

假设您的窗口小部件跟踪选择了哪个项目,您需要将此属性动态添加到&lt;选项&gt;标记:

selected="selected"

这是一个完整的示例(基于您的代码):

<option <?php selected( $instance['posttype'], 'Document Icon'); ?> value="document" selected="selected">Document Icon</option>