2012-12-04 27 views
2

我一直在這個工作了一段時間,並沒有能夠找到一個答案在其他地方在線。WordPress的PHP - 從主題選項的兩個值

在我的wordpress選項面板中,我有一個textarea,我可以輸入,如果我想添加另一個textarea,我只需點擊添加並刪除它們。

這裏是什麼樣子:

Theme Options Example

我所有的textarea的選項放入數組,我可以在任何時候給他們打電話。

我想做什麼,並一直試圖找出一段時間現在是這樣的: 我希望能夠在每個textareas下面添加一個文本輸入,並且每當我更新它,它把每個文本輸入到一個數組中。

我有一種感覺,這只是那麼簡單,我還沒有想過。如果沒有,請讓我知道。

這裏是選項面板的github上:https://github.com/leemason/NHP-Theme-Options-Framework

下面是我一直在努力代碼:

此代碼是在一個名爲field_multi_textarea.php新的文件,我已經加入到一個名爲multi_text新的文件夾在選項文件夾中的fields目錄中。

<?php 
class NHP_Options_multi_textarea extends NHP_Options{ 

    /** 
    * Field Constructor. 
    * 
    * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function 
    * 
    * @since NHP_Options 1.0.5 
    */ 
    function __construct($field = array(), $value ='', $parent){ 

     parent::__construct($parent->sections, $parent->args, $parent->extra_tabs); 
     $this->field = $field; 
     $this->value = $value; 
     //$this->render(); 

    }//function 



    /** 
    * Field Render Function. 
    * 
    * Takes the vars and outputs the HTML for the field in the settings 
    * 
    * @since NHP_Options 1.0.5 
    */ 
    function render(){ 

     $class = (isset($this->field['class']))?$this->field['class']:'large-text'; 

     echo '<ul id="'.$this->field['id'].'-ul">'; 

     if(isset($this->value) && is_array($this->value)){ 
      foreach($this->value as $k => $value){ 
       if($value != ''){ 

        echo '<li><div class="fadeout"><textarea id="'.$this->field['id'].'-'.$k.'" name="'.$this->args['opt_name'].'['.$this->field['id'].'][]" rows="6" class="'.$class.'" />'.esc_attr($value).'</textarea></div> <a href="javascript:void(0);" class="nhp-opts-multi-textarea-remove">'.__('Remove', 'nhp-opts').'</a></li>'; 

       }//if 

      }//foreach 
     }else{ 

      echo '<li><div class="fadeout"><textarea id="'.$this->field['id'].'" name="'.$this->args['opt_name'].'['.$this->field['id'].'][]" rows="6" value="'.$this->field['std'].'" class="'.$class.'" />'.esc_attr($value).'</textarea></div> <a href="javascript:void(0);" class="nhp-opts-multi-textarea-remove">'.__('Remove', 'nhp-opts').'</a></li>'; 

     }//if 

     echo '<li style="display:none;"><div class="fadeout"><textarea id="'.$this->field['id'].'" name="" rows="6" class="'.$class.'" /></textarea></div> <a href="javascript:void(0);" class="nhp-opts-multi-textarea-remove">'.__('Remove', 'nhp-opts').'</a></li>'; 

     echo '</ul>'; 

     echo '<a href="javascript:void(0);" class="nhp-opts-multi-textarea-add" rel-id="'.$this->field['id'].'-ul" rel-name="'.$this->args['opt_name'].'['.$this->field['id'].'][]">'.__('Add More', 'nhp-opts').'</a><br/>'; 

     echo (isset($this->field['desc']) && !empty($this->field['desc']))?' <span class="description">'.$this->field['desc'].'</span>':''; 

    }//function 


    /** 
    * Enqueue Function. 
    * 
    * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 
    * 
    * @since NHP_Options 1.0.5 
    */ 
    function enqueue(){ 

     wp_enqueue_script(
      'nhp-opts-field-multi-textarea-js', 
      NHP_OPTIONS_URL.'fields/multi_textarea/field_multi_textarea.js', 
      array('jquery'), 
      time(), 
      true 
     ); 

    }//function 

}//class 
?> 

這是我命名爲field_multi_textarea.js,放在multi_text文件夾中的JS。

jQuery(document).ready(function(){ 

    jQuery('.nhp-opts-multi-textarea-remove').live('click', function(){ 
     jQuery(this).prev('.fadeout').val(''); 
     jQuery(this).parent().fadeOut('slow', function(){jQuery(this).remove();}); 
    }); 

    jQuery('.nhp-opts-multi-textarea-add').click(function(){ 
     var new_input = jQuery('#'+jQuery(this).attr('rel-id')+' li:last-child').clone(); 
     jQuery('#'+jQuery(this).attr('rel-id')).append(new_input); 
     jQuery('#'+jQuery(this).attr('rel-id')+' li:last-child').removeAttr('style'); 
     jQuery('#'+jQuery(this).attr('rel-id')+' li:last-child textarea').val(''); 
     jQuery('#'+jQuery(this).attr('rel-id')+' li:last-child textarea').attr('name' , jQuery(this).attr('rel-name')); 
    }); 

}); 

我呼籲這個功能在NHP-options.php這樣的:

array(
    'id' => 'cf_emailaddress2', 
    'type' => 'multi_textarea', 
    'title' => __('Contact Form Email Address', 'nhp-opts'), 
    'sub_desc' => __('To add more, click add more', 'nhp-opts'), 
    'et_url' => 'myurl', 
    'std' => get_bloginfo('admin_email') ."", 
    'desc' => __('Contact Form Email Address, This is where the form will be sent to.', 'nhp-opts') 
    ), 

我可以化妝輸入看起來像下面的圖片,但其價值犯規保存,或者是與textarea中的任何內容相同。

enter image description here

任何幫助,將不勝感激。謝謝

+1

試圖複製你的設置,但沒有運氣。你確定你的工作代碼是完整的嗎? – brasofilo

+0

它似乎是...我已經在我的主題上安裝了這個:https://github.com/leemason/NHP-Theme-Options-Framework ..我在名爲'multi_textarea'的字段目錄中添加了一個新文件夾。 。我將文件命名爲'field_multi_textarea.php'和js'field_multi_textarea.js'。我有js隱藏和顯示div'fadeout'而不是li文本......並且在NHP-options.php中,我通過'type =>'multi_textarea'調用了這個 – JCBiggar

+0

這個信息屬於問題。 ::::: **我也**添加了NHP到主題:** 1)**使用了'get_template_part('nhp','options');'按照文檔。出現一堆菜單。** 2)**將你的代碼添加到'functions.php'中,沒有任何反應。 ** 3)**實例化類,沒有任何東西。 ** 4)**從類中啓用'$ this-> render()',它打印出破壞管理佈局的東西。 ** 5)**結論:您沒有提供所有代碼來複制該問題,因此無法回答.... – brasofilo

回答

0

檢查您的TextArea字段名稱。我猜他們是一樣的,在保存時如何訪問字段 看到下面這行的名字形成

echo '<li><div class="fadeout"><textarea id="'.$this->field['id'].'-'.$k.'" name="'.$this->args['opt_name'].'['.$this->field['id'].'][]" rows="6" class="'.$class.'" />'.esc_attr($value).'</textarea></div> <a href="javascript:void(0);" class="nhp-opts-multi-textarea-remove">'.__('Remove', 'nhp-opts').'</a></li>'; 

類似於你可能需要添加$ k作爲前綴taxtarea ID。

相關問題