2013-08-25 104 views
2

UPDATE:看起來他們正在保存,但當窗體在admin(保存後)重新加載時,當前值不會顯示在下拉框中。我有「選擇」的行,但我不知道我錯過了什麼,以便讓他們顯示當前值。自定義小部件不會將值保存到數據庫

我正在嘗試創建一個小部件,它允許您從兩個單獨的下拉菜單中選擇一個CTA和一個着陸頁。該下拉列表是基於兩個自定義帖子類型,它工作正常。我的問題是,我無法弄清楚如何將值保存到數據庫中(即,單擊保存在管理器中的小部件不會將值提交到數據庫中。)

這裏是我的相關函數widget類稱爲cta_widget。(我離開了代號爲第二下拉,因爲它是相同的)。

<?php 
function update($new_instance, $old_instance) 
{ 
    $instance = $old_instance; 

    /* Strip tags (if needed) and update the widget settings. */ 
    $instance['cta_id'] = $new_instance['cta_id']; 
    $instance['url_id'] = $new_instance['url_id']; 

    return $instance; 
} 

function form($instance) { 
    /* Set up some default widget settings. */ 
    $defaults = array('cta_id' => '23', 'url_id' => '28'); 
    $instance = wp_parse_args((array)$instance, $defaults); 
?> 
<p> 
    <label 
     for="<?php echo $this->get_field_name('cta_id'); ?>"><?php _e('CTA:'); ?></label> 

    <select name="<?php echo $this->get_field_name('cta_id'); ?>" 
      id="cta_id<?php echo $this->get_field_name('cta_id'); ?>" 
      style="width:100%;"> 
     <option value=""></option> 

     <?php 

     $posts = get_posts(
      array(
       'post_type' => 'locable_ctas', 
       'numberposts' => -1 
      ) 
     ); 
     if ($posts) { 

      foreach ($posts as $p) { 
       if ($p == $selected) { 
        $selected = "selected = 'selected'"; 
       } else { 
        $selected = ""; 
       } 
       echo '<option value="' . $p->ID . '" ' . $selected . '>' . $p->post_title . '</option>'; 
      } 
     } 

     ?> 
    </select> 
</p> 
} 
+0

找出它,下面是正確的代碼: –

回答

0

我想通了,這是其他任何人有同樣的問題的代碼。

function form($instance) { 

     $cta = $instance['cta_id']; 
     $lp = $instance['lp_id'];  

     /* Set up some default widget settings. */ 
     $defaults = array('cta_id' => '', 'lp_id' => ''); 
     $instance = wp_parse_args((array) $instance, $defaults); 

     ?> 

     <p> 

     <?php echo 'CTA: '.$cta;?> 
     <?php echo 'LP: '.$lp.'<br/>';?> 

     <label for="<?php echo $this->get_field_name('cta_id'); ?>"><?php _e('CTA:'); ?></label> 

      <select name="<?php echo $this->get_field_name('cta_id'); ?>" id="cta_id<?php echo $this->get_field_name('cta_id'); ?>" style="width:100%;"> 
      <option value=""></option> 

      <?php 

      $posts = get_posts(
       array(
        'post_type' => 'locable_ctas', 
        'numberposts' => -1 
       ) 
      ); 
      if($posts) 
      { 

       foreach($posts as $p) 
       { 
        if ($p->ID==$cta) 
        { 
         $is_selected = "selected = 'selected'"; 
        } 
        else 
        { 
         $is_selected = ""; 
        } 
        echo '<option value="' . $p->ID . '" '.$is_selected.'>' .$p->post_title . '</option>'; 
       } 
      } 

     ?> 
     </select> 

     </p> 

     <p> 

     <label for="<?php echo $this->get_field_name('lp_id'); ?>"><?php _e('LP:'); ?></label> 

      <select name="<?php echo $this->get_field_name('lp_id'); ?>" id="<?php echo $this->get_field_name('lp_id'); ?> style="width:100%;""> 
      <option value=""></option> 

      <?php 

      $posts = get_posts(
       array(
        'post_type' => 'locable_lps', 
        'numberposts' => -1 
       ) 
      ); 
      if($posts) 
      { 
       foreach($posts as $p) 
       { 
        if ($p->ID==$lp) 
        { 
         $is_selected = "selected = 'selected'"; 
        } 
        else 
        { 
         $is_selected = ""; 
        } 
        echo '<option value="' . $p->ID . '" '.$is_selected.'>' .$p->post_title . '</option>'; 
       } 
      } 

     ?> 
     </select> 

     </p>    

     <?php 
    } 
相關問題