$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - How to upload 3 attachments to current post?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - How to upload 3 attachments to current post?

matteradmin9PV0评论

I have a form which updates a post but when updating I need to be able to also upload 3 posts:

<div class="form-group">
    <input class="form-control" type="file" tabindex="3" name="custom-upload1" id="custom-upload2" />
    <input class="form-control" type="file" tabindex="3" name="custom-upload2" id="custom-upload2" />
    <input class="form-control" type="file" tabindex="3" name="custom-upload3" id="custom-upload3" />
</div>

The post I am updtaing is a draft which I then make it publish and within the same form I need to be able to also change the title, so within the same template I also have:

<div class="form-group">
    <input id="theTitle" class="form-control" type="hidden" name="inputTitle" value="<?php echo $myTitle; ?>" autocomplete="nope">
</div>
<?php if ($post->post_author == $current_user->ID) {
    toPubblish($post->ID); ?>
    <input type="hidden" value="ok" name="pubblish">
    <?php
        if ( get_post_status ($post->ID) == 'publish' ) {
        } else { ?>
            <input id="upTitle" type="submit" name="newTitle" value="BUONA SERATA" class="btn secondary-btn primary-bg">
    <?php }
} ?>

And then I do

if($_SERVER['REQUEST_METHOD']=="POST") {
    if ('BUONA SERATA' === ($_POST['newTitle'])) {

        $post = array(
            'ID' => $id,
            'post_title' => $_POST['inputTitle']
        );
        wp_update_post($post, true); 

        if ($_FILES['postImage']) {
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
               //Add your error action
                } else {
                    $attach_id = media_handle_upload( $file, $post->ID );
                }
            }   
        }
        if ($attach_id > 0){
          update_post_meta($post,'bgc_event_pimg',$attach_id);
        }

        ob_start(); //this should be first line of your page
        $myurl = get_site_url();
        header('Location: '.$myurl."/profile/");
        ob_end_flush(); //this should be last line of your page
    }
}

And in function I have:

function insert_attachment($file_handler,$post_id,$setthumb='false') {

  // check to make sure its a successful upload
  if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

  require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  require_once(ABSPATH . "wp-admin" . '/includes/file.php');
  require_once(ABSPATH . "wp-admin" . '/includes/media.php');

  $attach_id = media_handle_upload( $file_handler, $post_id );

  if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
  return $attach_id;
}

But the images are uploading but are not attaching to current posts and I cannot see them if I see the posts

Post a comment

comment list (0)

  1. No comments so far