WordPress,我怎样才能获得我上传的图片的ID?

时间:2015-12-19 08:13:11

标签: 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>

2 个答案:

答案 0 :(得分:0)

我希望我能正确理解这个问题。您已将图像上传到Wordpress并想要该图像的ID或URL?

网址很简单。只需转到上传库并覆盖图像,您就会看到该网址。

关于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/

答案 1 :(得分: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。