【问题标题】:Wordpress widget <select> not displaying when savedWordpress 小部件 <select> 保存时不显示
【发布时间】:2014-01-15 14:04:14
【问题描述】:

我正在尝试创建一个自定义 Wordpress 小部件,它有一个下拉选择和文本输入字段。一切正常,但是当我返回小部件(例如编辑)时,选择下拉菜单始终显示顶部结果而不是我的选择。

// 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");') );

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress widget


    【解决方案1】:

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

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

    同样更改其他选项的值。它在我的 wordpress 安装上运行良好!

    【讨论】:

      【解决方案2】:

      假设您的小部件跟踪已选择的项目,您需要将此属性动态添加到

      selected="selected"
      

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

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

      【讨论】:

      • selected() 函数将根据保存的值自动添加它。请检查我的答案以获得正确的解决方案
      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多