我有一个前端帖子表单,允许用户上传多张照片,在那里我有2个输入字段,用于尝试保存每个附件的自定义元数据-
$vid_pix = get_post_meta($v_Id, \'vid_pix\', false);
if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
$Pt = get_post_meta($vP, \'photo_time\', true);
$Por = get_post_meta($vP, \'photo_order\', true);
echo \'<img src="\' . wp_get_attachment_thumb_url($vP) . \'" alt=""/>\';
echo \'<input type="hidden" name="photo_order" class="photo_order" value="\'.$Por.\'" />\';
echo \'<input type="text" name="photo_time" class="photo_time" value="\'.$Pt.\'"/>\';
}
}
提交表单时,我尝试使用以下方法保存表单(我注释掉了失败的尝试)-
//$Pt = get_post_meta($v_Id, \'photo_time\', false);
//$Por = get_post_meta($v_Id, \'photo_order\', false);
// GET ATTACHMENT IDS
if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
$post_id = $vP;
$meta = $_POST[\'attachments\'][ $post_id ][\'photo_time\'];
update_post_meta($post_id, \'photo_time\', $meta);
$meta_two = $_POST[\'attachments\'][$post_id][\'photo_order\'];
update_post_meta($post_id, \'photo_order\', $meta_two);
update_post_meta($vP, \'photo_time\', $attachment[\'attachments\'][$vP][\'photo_time\']);
update_post_meta($vP, \'photo_order\', $attachment[\'attachments\'][$vP][\'photo_order\']);
//$Pt = get_post_meta($v_Id, \'photo_time\', false);
//$Por = get_post_meta($v_Id, \'photo_order\', false);
//$Pt = get_post_meta($vP, \'photo_time\', true);
//$Por = get_post_meta($vP, \'photo_order\', true);
//update_post_meta($vP, \'photo_time\', $Pt);
//update_post_meta($vP, \'photo_order\', $Por);
//update_post_meta($v_Id, \'_photo_time\', $newvidPix);
//add_post_meta($v_Id, \'photo_time\', $Pt, false);
然而,我无法让它工作。在函数中使用以下代码。php我可以在后端完成所有工作--
// ADD FIELDS
function mytheme_attachment_fields( $fields, $post ) {
$meta = get_post_meta($post->ID, \'photo_time\', true);
$fields[\'photo_time\'] = array(
\'label\' => \'photo time\',
\'input\' => \'text\',
\'value\' => $meta,
\'show_in_edit\' => true,
);
$meta_two = get_post_meta($post->ID, \'photo_order\', true);
$fields[\'photo_order\'] = array(
\'label\' => \'photo order\',
\'input\' => \'text\',
\'value\' => $meta_two,
\'show_in_edit\' => true,
);
return $fields;
}
add_filter( \'attachment_fields_to_edit\', \'mytheme_attachment_fields\', 10, 2 );
// Update custom field on save
function mytheme_update_attachment_meta($attachment){
global $post;
update_post_meta($post->ID, \'photo_time\', $attachment[\'attachments\'][$post->ID][\'photo_time\']);
update_post_meta($post->ID, \'photo_order\', $attachment[\'attachments\'][$post->ID][\'photo_order\']);
return $attachment;
}
add_filter( \'edit_attachment\', \'mytheme_update_attachment_meta\', 4);
// Update custom field via ajax
function mytheme_media_xtra_fields() {
$post_id = $_POST[\'id\'];
$meta = $_POST[\'attachments\'][$post_id ][\'photo_time\'];
update_post_meta($post_id , \'photo_time\', $meta);
$meta_two = $_POST[\'attachments\'][$post_id ][\'photo_order\'];
update_post_meta($post_id , \'photo_order\', $meta_two);
clean_post_cache($post_id);
}
add_action(\'wp_ajax_save-attachment-compat\', \'mytheme_media_xtra_fields\', 0, 1);
如何在前端节省开支,有什么想法吗?谢谢
最合适的回答,由SO网友:Sally CJ 整理而成
在后端,您检索的数据如下所示:
$meta = $_POST[\'attachments\'][ $post_id ][\'photo_time\'];
$meta_two = $_POST[\'attachments\'][$post_id][\'photo_order\'];
因此,在前端,您应该设置字段的
name
在此格式中:
attachments[<?= $vP ?>][KEY]
哪里
KEY
可能是
photo_order
或
photo_time
.
echo \'<input type="hidden" name="attachments[<?= $vP ?>][photo_order]" class="photo_order" value="\'.$Por.\'" />\';
echo \'<input type="text" name="attachments[<?= $vP ?>][photo_time]" class="photo_time" value="\'.$Pt.\'"/>\';
UPDATE
保存数据:
检查中的代码UPDATE #2 下文第节
使用该代码代替您(尝试过的)代码&mdash;您的问题中这之后的代码:
提交表单时,我尝试使用以下方法保存表单(我注释掉了失败的尝试)-
UPDATE #2
最好将代码(用于保存数据)包装到函数中,如下所示:
add_action( \'init\', \'my_save_vid_pix_data\' );
function my_save_vid_pix_data() {
if ( ! isset( $_POST[\'attachments\'], $_POST[\'v_Id\'] ) ) {
return;
}
$atts = (array) $_POST[\'attachments\'];
$post_id = absint( $_POST[\'v_Id\'] );
$vid_pix = get_post_meta($post_id, \'vid_pix\', false);
foreach ((array) $vid_pix as $vP) {
if ( ! isset( $atts[ $vP ] ) || ! is_array( $atts[ $vP ] ) ) {
continue;
}
update_post_meta($vP, \'photo_time\', $atts[ $vP ][\'photo_time\']);
update_post_meta($vP, \'photo_order\', $atts[ $vP ][\'photo_order\']);
}
}
(将该功能添加到主题的
functions.php
文件。)
并添加隐藏的input
字段(已命名v_Id
) 在此之后foreach
回路:
if (!empty($vid_pix)) {
foreach ($vid_pix as $vP) {
...
echo \'<input type="text" name="attachments[<?= $vP ?>][photo_time]" class="photo_time" value="\'.$Pt.\'"/>\';
}
echo \'<input type="hidden" name="v_Id" value="\' . $v_Id . \'">\';
}
(有关
$v_Id
变量。)