get_field_id
为字段id准备前缀,在一个小部件实例中总是相同的。表单字段可能类似于以下内容:
// -- $element[0] --
// $field[icontxt]
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][0]" value="%4$s">
// $field[iconlnk]
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][0]" value="%4$s">
//$field[iconlnktrgt]
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][0]" value="%4$s">
// -- $element[1] --
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][1]" value="%4$s">
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][1]" value="%4$s">
<input class="widefat" id="widget-baseid-instance-inner_elements" name="widget-baseid[instance][inner_elements][1]" value="%4$s">
您需要为每个字段使用不同的id/名称
此外,对字段组(txt、lnk和lnktrgt)进行编号,并将此编号添加到字段ID的末尾。单击“+”按钮后应执行相同操作。
$fields[\'icontxt\'] = sprintf(
\'<input class="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">\',
$this->get_field_id(\'icontxt\' . $elements_counter),
$this->get_field_name(\'icontxt\'),
$elements_counter,
esc_attr($inner_elements[\'icontxt\'])
);
$fields[\'iconlnk\'] = sprintf(
\'<input class="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">\',
$this->get_field_id(\'iconlnk\' . $elements_counter),
$this->get_field_name(\'iconlnk\'),
$elements_counter,
esc_attr($inner_elements[\'iconlnk\'])
);
$fields[\'iconlnktrgt\'] = sprintf(
\'<input class="widefat" id="%1$s" name="%2$s[%3$s]" value="%4$s">\',
$this->get_field_id(\'iconlnktrgt\' . $elements_counter),
$this->get_field_name(\'iconlnktrgt\'),
$elements_counter,
esc_attr($inner_elements[\'iconlnktrgt\'])
);
<input class="widefat" id="widget-baseid-instance-icontxt0" name="widget-baseid[instance][icontxt][0]" value="%4$s">
<input class="widefat" id="widget-baseid-instance-iconlnk0" name="widget-baseid[instance][iconlnk][0]" value="%4$s">
<input class="widefat" id="widget-baseid-instance-iconlnktrgt0" name="widget-baseid[instance][iconlnktrgt][0]" value="%4$s">
不应
$elements[$element_num]
在这里吗?您只留下一个索引。
$elements_num = count($elements);
$elements[$element_num + 1] = \'\';
<小时>
Update英寸
update()
作用
$new_instance
参数包含中使用的键
form()
- \'icontxt“,”iconlnk“,”iconlnktrgt“:
$new_instance[icontxt][
0 => "some value from field no. 1",
1 => "value from field no. 2",
];
$new_instance[iconlnk][
0 => "link from first field ",
1 => "second link",
];
从字段读取数据
//name= $this->get_field_name(\'icontxt\')" ...>
<input class="widefat" id="..." name="" ...>
内部
update()
功能使用
$value = $new_instance[\'icontxt\'][0];
.
您可以尝试这样的代码将表单数据收集到数组中$elements[\'inner_elements\']
:
// arrays initialization
$tmp_elements = [ \'inner_elements\' => [] ];
if ( isset($new_instance[\'icontxt\']) ) {
for($i = 0; $i < count( $new_instance[\'icontxt\'] ); ++$i)
$tmp_elements[\'inner_elements\' . $i][\'icontxt\'] = $new_instance[\'icontxt\'][$i];
unset($new_instance[\'icontxt\']);
}
// ...
$new_instance[\'elements\'] = $tmp_elements;