2017-10-10 41 views
0
<div class="modal-body"> 
     <form method="POST" action=""> 
      <input type="file" name="featured_img"><br/> 
      <input type="submit" name="imgupdate" class="btn btn-info btn-lg"> 
      <button type="button" class="btn btn-info btn-lg" data-dismiss="modal">Cancel</button> 
     </form> 

    </div> 


<?php 
    global $wpdb; 
    $update_id = get_the_ID(); 

    // Update featured image 

    $uploaddir = wp_upload_dir(); 
    $file  = $_FILES['featured_img']; 
    $uploadfile = $uploaddir['path'] . '/' . basename($file['name']); 
    move_uploaded_file($file['tmp_name'], $uploadfile); 
    $filename = basename($uploadfile); 
    $wp_filetype = wp_check_filetype(basename($filename), null); 
    $attachment = array(
     'post_mime_type' => $wp_filetype['type'], 
     'post_title' => preg_replace('/\.[^.]+$/', '', $filename), 
     'post_content' => '', 
     'post_status' => 'inherit', 
     'menu_order' => $_i + 1000 
    ); 
    $update_img = wp_insert_attachment($attachment, $uploadfile); 
    if (isset($_POST['imgupdate'])) { 
     if (($_FILES['featured_img']['name'] == true)) { 
      update_post_meta($update_id, '_thumbnail_id', $update_img); 
     } 
    } 

?> 

我想從前端編輯特色圖像我想通過使用此代碼來實現此目的,但它不工作,它不更新圖像。任何幫助?我在哪裏得到這個問題。此代碼的其他頁面上做工精細。但我想這個模型彈出。這不適用於彈出窗口。任何幫助?上傳中的問題在前端自定義帖子類型中的功能

在此先感謝。

+0

你使用的顯示您的彈出一個插件?我們可以有完整的源代碼? – Paul

回答

0

你可以這樣添加的代碼,它是爲我工作。

希望這也會爲你工作。

if ($_FILES) { 
     array_reverse($_FILES); 
     if ($_FILES['file_featured_image']) { 
      $i = 0; //this will count the posts 
      foreach ($_FILES as $file => $array) { 
       if ($i == 0) 
        $set_feature = 1; //if $i ==0 then we are dealing with the first post 
       else 
        $set_feature = 0; //if $i!=0 we are not dealing with the first post 

       insert_featured_image_for_property($file, $property_id, $set_feature); 
       $i++; //count posts 
      } 
     } 
    } 

添加功能的functions.php

function insert_featured_image_for_property($file_handler, $property_id, $setthumb = 'false') { 
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) { 
    return __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, $property_id); 
//set post thumbnail if setthumb is 1 
if ($setthumb == 1) 
    update_post_meta($property_id, '_thumbnail_id', $attach_id); 
return $attach_id; 
} 
相關問題