我正在尝试创建多个自定义用户配置文件字段,以允许我们的用户在其配置文件页面中添加更多软件技能。
我面临的问题是,它总是只为“skills"
和一个元数据"skills_percent"
即使我添加了更多的输入!
当我添加更多字段时,应该如何保存所有字段?
以及如何在前端显示它们!
这就是我所做的。<table class="skills-table">
<tr>
<th><label for="skills"><?php _e("Skills"); ?></label></th>
<td>
<div class="multi-field-wrapper">
<button type="button" class="add-field">+</button>
<div class="multi-fields">
<div class="multi-field">
<input type="text" name="skills" id="skills" value="<?php echo esc_attr( get_the_author_meta( \'skills\', $current_user->ID ) ); ?>" class="textbox" />
<input type="text" name="skills_percent" id="skills_percent" value="<?php echo esc_attr( get_the_author_meta( \'skills_percent\', $current_user->ID ) ); ?>" class="textbox" /><br />
<button type="button" class="remove-field">-</button>
</div>
</div>
</div>
<span class="description"><?php _e("Please enter your programs or software in the first box and how many percent in the second box ."); ?></span>
</td>
</tr>
</table>
的脚本adding/removing
字段动态。<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'.multi-field-wrapper\').each(function() {
var $wrapper = $(\'.multi-fields\', this);
$(".add-field", $(this)).click(function(e) {
$(\'.multi-field:first-child\', $wrapper).clone(true).appendTo($wrapper).find(\'input\').val(\'\').focus();
});
$(\'.multi-field .remove-field\', $wrapper).click(function() {
if ($(\'.multi-field\', $wrapper).length > 1)
$(this).parent(\'.multi-field\').remove();
});
});
});
</script>
更新用户元if ( !empty( $_POST[\'skills\'] ) )
update_user_meta( $current_user->ID, \'skills\', esc_attr( $_POST[\'skills\'] ) );
if ( !empty( $_POST[\'skills_percent\'] ) )
update_user_meta( $current_user->ID, \'skills_percent\', esc_attr( $_POST[\'skills_percent\'] ) );
在前端显示作者元<dt class="j-s-m">Skills</dt><dd class="text-intro"><?php echo get_the_author_meta(\'skills\', $curauth->ID); ?> <?php echo get_the_author_meta(\'skills_percent\', $curauth->ID); ?></dd>
Update 1 as mrben522答案if ( !empty( $_POST[\'skills\'] ) )
$skillArray = [];
if (is_array($_POST[\'skills\'])) {
foreach ($_POST[\'skills\'] as $skill) {
$skillArray[] = esc_attr($skill);
}
} else {
$skillArray[] = esc_attr($_POST[\'skills\']);
}
update_user_meta( $current_user->ID, \'skills\', $skillArray );
if ( !empty( $_POST[\'skills_percent\'] ) )
$skills_percentArray = [];
if (is_array($_POST[\'skills_percent\'])) {
foreach ($_POST[\'skills_percent\'] as $skill_percent) {
$skills_percentArray[] = esc_attr($skill_percent);
}
} else {
$skills_percentArray[] = esc_attr($_POST[\'skills_percent\']);
}
update_user_meta( $current_user->ID, \'skills_percent\', $skills_percentArray );
但它会返回技能和技能百分比的数组数组,也不会保存"multi-field"