我的页面上有一个多文件上传,可以在自定义字段中保存帖子附件---
if (!empty($_FILES[\'vidPix\'][\'tmp_name\'][0])) {
$i = 1;
$files = $_FILES[\'vidPix\'];
foreach ($files[\'name\'] as $key => $value) {
if ($files[\'name\'][$key]) {
$file = array(
\'name\' => $files[\'name\'][$key],
\'type\' => $files[\'type\'][$key],
\'tmp_name\' => $files[\'tmp_name\'][$key],
\'error\' => $files[\'error\'][$key],
\'size\' => $files[\'size\'][$key]
);
$_FILES = array("sight" . $i => $file);
add_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
add_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');
$mfile = wp_handle_upload($files, $upload_overrides );
$newvidPix = sight("sight" . $i, $v_Id);
remove_filter( \'upload_dir\', \'wpse_141088_upload_dir\' );
remove_filter(\'intermediate_image_sizes_advanced\', \'no_image_resizing\');
if ($i == 1) {
update_post_meta($v_Id, \'_thumbnail_id\', $newvidPix);
}
add_post_meta($v_Id, \'vid_pix\', $newvidPix, false);
}
$i++;
}
}
我试图在前端使用delete按钮和ajax来删除post附件(这在foreach语句中,“$vP”是附件ID-
echo \'<input type="hidden" id="att_remove" name="att_remove[]" value="\'. $vP .\'" /><input type="hidden" name="nonce" id="nonce" value="\' . wp_create_nonce( \'delete_attachment\' ) . \'" /></div>\';
关于我的功能。php页面我有-
function delete_attachment( $post ) {
$id = $_POST[\'att_ID\'];
$v_Id = $_POST[\'post_ID\'];
$vid_pix = get_post_meta($v_Id, \'vid_pix\', true);
delete_post_meta($id, $vP);
delete_post_meta($id, $vid_pix);
delete_post_meta($v_Id, $vid_pix, $id);
wp_delete_attachment( $id, $force_delete );
update_post_meta($v_Id, \'vid_pix\');
die();
}
到目前为止,它删除了图像,但没有删除附件帖子。因此,ID仍然显示在我的foreach循环等中,但图像已成功删除。
我如何才能完全删除附件而不仅仅是文件?