【发布时间】:2012-12-04 20:22:15
【问题描述】:
我已经为此工作了一段时间,但无法在网上其他任何地方找到答案。
在我的 wordpress 选项面板中,我有一个可以输入的 textarea,如果我想添加另一个 textarea,我只需单击以添加和删除它们。
这是它的样子:
我所有的 textarea 选项都放在一个数组中,我可以随时调用它们。
我想要做的,并且一直试图弄清楚现在是这样的: 我希望能够在每个文本区域下方添加一个文本输入,并且每当我更新它时,它会将每个文本输入放入一个数组中。
我有一种感觉,这只是一件我还没有想到的简单的事情。如果没有,请告诉我。
这里是选项面板的 github:https://github.com/leemason/NHP-Theme-Options-Framework
以下是我一直在处理的代码:
此代码位于名为 field_multi_textarea.php 的新文件中,我已将其添加到选项文件夹中 fields 目录内的名为 multi_text 的新文件夹中。
<?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')
),
我可以使输入看起来像下面的图像,但它的值没有保存,或者它与文本区域中的任何内容相同。
任何帮助将不胜感激。谢谢
【问题讨论】:
-
尝试复制您的设置,但没有成功。你确定你的工作代码是完整的吗?
-
似乎...我已经在我的主题上安装了这个:github.com/leemason/NHP-Theme-Options-Framework .. 我在名为
multi_textarea的字段目录中添加了一个新文件夹。我将文件本身命名为@ 987654334@ 和 jsfield_multi_textarea.js。我有 js 来隐藏和显示 divfadeout而不是 li 文本......在 NHP-options.php 中,我通过type => 'multi_textarea调用它 -
此信息属于问题的正文。 ::::: 我还在主题中添加了 NHP:1) 按照文档使用
get_template_part('nhp', 'options');。出现一堆菜单。 2) 将您的代码添加到functions.php,没有任何反应。 3) 实例化了类,什么都没有。 4) 从类中启用$this->render(),它会打印破坏管理布局的内容。 5) 结论:您没有提供复制问题的所有代码,因此无法回答.... -
感谢您告诉我。我用更多的代码更新了我的答案。我对
$this->render()函数感到困惑? -
克隆一个或多个输入时,请确保检查它们的 ID 是否相同。使用 firebug 检查您的新输入及其 ID。在我看来,您的 ID 在 javascript 中是相同的。