我使用以下内容发布新文件并正常工作:
HTML
<div class="row">
<div class="col-xs-12">
<form method="POST" action="" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="postImage" multiple="multiple" class="form-control">
</div>
<input id="uploadImg" name="uploadImgCustom" type="submit" value="AGGIORNA ALLEGATO" class="btn secondary-btn primary-bg">
</form>
</div>
</div>
上传逻辑
$myNewImg = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['uploadImgCustom'];
}
$attachments = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' =>'any',
'post_parent' => $id
));
if ($attachments) {
foreach ( $attachments as $attachment ) {
$myNewImg = wp_get_attachment_url( $attachment->ID );
update_post_meta( $id, 'usp-file-single', $myNewImg);
}
$pathtofile = $myNewImg;
$info = pathinfo($pathtofile);
if ( ($info["extension"] == "jpg") || ($info["extension"] == "png") ) { ?>
<a href="<?php the_permalink(); ?>">
<img src="<?php echo $myNewImg; ?>" class="bnr_img img-responsive center-block" alt="">
</a>
<?php } else {
// Get the Video Fields
$video_mp4 = get_post_meta($id, 'usp-file-single', TRUE);
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'webm' => $video_webm,
'flv' => $video_flv,
'poster' => $video_poster,
'preload' => 'auto',
'autoplay' => "off"
);
// Display the Shortcode
echo wp_video_shortcode( $attr );
}
}
POST CHECK
if($_SERVER['REQUEST_METHOD']=="POST") {
if ('AGGIORNA ALLEGATO' === ($_POST['uploadImgCustom'])) {
if ($_FILES['postImage']) {
$attachments = get_attached_media( '', $id );
foreach ($attachments as $attachment) {
wp_delete_attachment( $attachment->ID, 'true' );
}
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
//Add your error action
} else {
$attach_id = media_handle_upload( $file, $id );
$myNewImg = get_post_meta($id, 'usp-file-single', true);
}
}
}
}
}
我试图将以下内容放在html表单之前
$myNewImg = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['uploadImgCustom'];
}
但是,在POST之后页面刷新,但是我看到了旧值,如果在帖子刷新后手动刷新,则可以看到新值。我在错误地使用if isset
吗?
答案 0 :(得分:1)
由于有评论建议,我最终创建了这样的页面刷新:
<?php
$myNewUploaded = get_post_meta($id, 'usp-file-single', true);
if (isset($_POST['uploadImgCustom'])) {
$myNewImg = $_POST['postImage'];
ob_start(); //this should be first line of your page
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush(); //this should be last line of your page
}
?>
<form method="POST" action="" enctype="multipart/form-data">.....
答案 1 :(得分:0)
尝试使用此
running task, with exception...bad_function_call