从前端更新附件不会显示新更改

时间:2018-11-23 作者:rob.m

我正在向db提交图像,如下所示:

if (isset($_POST[\'uploadImgCustom\'])) {
    $myNewImg = get_post_meta($id, \'usp-file-single\', true);
}
$attachments = get_posts(array( 
    \'post_type\' => \'attachment\',
    \'numberposts\' => -1,
    \'post_status\' =>\'any\',
    \'post_parent\' => $post->ID
));

if ($attachments) {
    foreach ( $attachments as $attachment ) {
        $myNewImg = wp_get_attachment_url( $attachment->ID );
        update_post_meta( $id, \'usp-file-single\', $myNewImg);
    } 
?>
    <img src="<?php echo $myNewImg; ?>" class="img-responsive">
<?php }
然后我会:

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 );
                }
            }   
        }
    }
}
新文件已正确上载,页面在上载完成后会自动刷新。然而,我在页面上看到了旧图像,但是如果我刷新页面,我会看到新文件。这就像保留缓存,而不是一开始就读取新数据。

下面是我在开始循环后在代码开头获取的当前帖子ID。

$id = get_the_ID();
html格式如下:

<div class="row">
    <div class="col-xs-12">
        <h3>CARICA UNA NUOVA IMMAGINE O VIDEO</h3>
        <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>

1 个回复
SO网友:rob.m

我就是这样解决的:

$myNewImg = get_post_meta($id, \'usp-file-single\', true);
if (isset($_POST[\'uploadImgCustom\'])) {
    $myNewImg = $_POST[\'uploadImgCustom\'];
}
$attachments = get_posts(array(.... 

结束

相关推荐

ADD_THEME_SUPPORT(‘CUSTOM-HEADER-UPLOADS’);功能有什么作用?

同时向functions.php 文件中,我发现了以下内容:add_theme_support(\'custom-header-uploads\');这到底是做什么的?我删除了条目functions.php 但对网站没有什么不同。我最初认为这与上传标题图像的能力有关,但这取决于add_theme_support(\'custom-header\');在执行搜索时,我无法在Codex中看到任何有意义的信息。这是某种贬值的特征还是我忽略了什么?