2015-12-19 111 views
-1

我需要上傳圖片並使用它。但我找不到圖片的ID。我現在能做什麼?WordPress,我怎樣才能得到我上傳的圖片的ID?

<form class="avatar-form" name="avatar_form" method="post" enctype="multipart/form-data"> 
    <input type="file" class="avatar" name="avatar" /> 
    <input type="submit" class="avatar-submit" name="avatar_submit" /> 
</form> 
+0

或者如果有人可以得到上傳圖片的網址? – lizs

回答

0
// get the file suffix 
    $suffix = pathinfo($file['name'], PATHINFO_EXTENSION); 
    // define the save location and filename 
    $filename = $user_name.'-'.date('YmdHis').'.'.$suffix; 
    $wp_upload_dir = wp_upload_dir(); 
    $path = $wp_upload_dir['path'].'/'.$filename; 

    // upload file 
    if(move_uploaded_file($file['tmp_name'], $path)) { 
     $attach_id = wp_insert_attachment(array(
      'guid'    => $wp_upload_dir['url'].'/'.$filename, 
      'post_mime_type' => $file['type'], 
      'post_title'  => preg_replace('/\.[^.]+$/', '', $filename), 
      'post_content'  => '', 
      'post_status'  => 'inherit', 
     ), $path, get_the_ID()); 

     // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. 
     require_once(ABSPATH . 'wp-admin/includes/image.php'); 

     // Generate the metadata for the attachment, and update the database record. 
     $attach_data = wp_generate_attachment_metadata($attach_id, $path); 
     wp_update_attachment_metadata($attach_id, $attach_data); 

    } 

我們可以上傳圖片作爲附件。這樣$ attach_id就是圖片ID。

0

我希望我已經正確地理解了這個問題。您是否已將圖片上傳到Wordpress,並希望獲得該圖片的ID或網址?

網址很簡單。只需進入上傳庫,然後在圖像上拖動即可看到網址。

關於ID它更復雜。 Google搜索後發現的一個建議是創建一個獲取ID的函數。

function pn_get_attachment_id_from_url($attachment_url = '') { 
    global $wpdb; 
    $attachment_id = false; 

    // If there is no url, return. 
    if ('' == $attachment_url) 
     return; 

    // Get the upload directory paths 
    $upload_dir_paths = wp_upload_dir(); 

    // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image 
    if (false !== strpos($attachment_url, $upload_dir_paths['baseurl'])) { 

     // If this is the URL of an auto-generated thumbnail, get the URL of the original image 
     $attachment_url = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachment_url); 

     // Remove the upload path base directory from the attachment URL 
     $attachment_url = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachment_url); 

     // Finally, run a custom database query to get the attachment ID from the modified attachment URL 
     $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url)); 

    } 

    return $attachment_id;} 

我還沒有嘗試過我的自我,但在這裏找到它; https://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/

+0

謝謝你的回答。我找到函數'wp_insert_attachment()',並解決它。 http://codex.wordpress.org/Function_Reference/wp_insert_attachment – lizs