2015-12-19 79 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