为什么克隆的值不会保存在DB中?对于第一行(不是克隆的那一行),它工作得很好,但当我使用jQuery(.clone)添加新值时,新添加的值不会保存。。。
jQuery
$(\'#add-embed\').on(\'click\', function() {
var row = $(\'.empty-row\').clone(true);
row.removeClass(\'empty-row\').addClass(\'entries\');
row.insertBefore(\'#repeater > div:last\');
return false;
});
HTML
<div id="repeater">
<div class="entries">
<input type="text" name="al_ttl[]">
<input type="text" name="al_tmp[]">
</div>
<div class="empty-row">
<input type="text" name="al_ttl[]">
<input type="text" name="al_tmp[]">
</div>
</div>
保存
function frontend_submit() {
global $current_user, $post;
if ( \'POST\' == $_SERVER[\'REQUEST_METHOD\']
&& !empty( $_POST[\'action\'] )
&& $_POST[\'action\'] == \'post\' ) {
//Global Post
$post_id = $post->ID;
$user_id = $current_user->ID;
$post_title = $_POST[\'post_title\'];
$post_id = wp_insert_post(array(
\'post_author\' => $user_id,
\'post_title\' => $post_title,
\'post_status\' => \'publish\',
\'post_type\' => \'test\'
));
//Data Rows Save
$old = get_post_meta($post_id, \'data_rows\', true);
$new = array();
$ah_ttl = $_POST[\'al_ttl\'];
$ah_tmp = $_POST[\'al_tmp\'];
$count = count( $ah_ttl );
for ( $i = 0; $i < $count; $i++ ) {
if ( $ah_ttl[$i] != \'\' ) :
$new[$i][\'al_ttl\'] = stripslashes( strip_tags( $ah_ttl[$i] ) );
$new[$i][\'al_tmp\'] = stripslashes( $ah_tmp[$i] );
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, \'data_rows\', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, \'data_rows\', $old );
}
}
do_action(\'wp_insert_post\', \'frontend_submit\');
最合适的回答,由SO网友:Andrea Somovigo 整理而成
再次检查您的<div id=\'repeater\'>
正确地位于内部<form>
页面的标记(我假设您处于表单中,因为您的代码不提供ajax功能)以及您输出的内容var_dump($_POST[\'al_ttl\'])
这可能是Undefined index: al_ttl
.
还要注意,您正在覆盖$post_id
具有wp_insert_post
之后,您将寻找post\\u meta$old = get_post_meta($post_id, \'data_rows\', true);
在新岗位上总是空的