2013-07-29 72 views
1

我似乎無法使用Wordpress媒體上傳來檢索特定圖片類型,如縮略圖,縮略圖或後縮略圖。使用Wordpress媒體上傳獲取圖片類型網址

我得到的只是圖像的原始大小url,因此我顯示的是一個巨大的圖像而不是縮略圖或定義的圖像大小。

這是我的腳本:

jQuery("#submit_logo_button").click(function(e){ 
e.preventDefault(); 
var custom_file_frame; 
if (typeof(custom_file_frame)!=="undefined") { 
    custom_file_frame.close(); 
    } 

    //Create WP media frame. 
    custom_file_frame = wp.media.frames.customHeader = wp.media({ 
    //Title of media manager frame 
    title: "Thumbs - Choose Logo", 
    library: { 
     type: 'image' 
    }, 
    button: { 
     //Button text 
     text: "Select Logo" 
    }, 
    size: "post-thumbnail",//shouldn't this work?!? 
    //Do not allow multiple files, if you want multiple, set true 
    multiple: false 
    }); 

    //callback for selected image 
    custom_file_frame.on('select', function() { 
    var attachment = custom_file_frame.state().get('selection').first().toJSON(); 
    jQuery("#image_thumbnail").attr("src", attachment.url); 

    }); 

    //Open modal 
    custom_file_frame.open(); 

});

三江源:)

回答

0

不是一個完整的awnser, 我不能讓自定義圖像尺寸,只有這些的下面列出。

console.log(attachment.sizes.thumbnail.url); 

//I can get any image type using: 

jQuery("#indot_under_logo_img").attr("src", attachment.sizes.thumbnail.url); 

//or 

jQuery("#indot_under_logo_img").attr("src", attachment.sizes.full.url); 

//or 

jQuery("#indot_under_logo_img").attr("src", attachment.sizes.medium.url); 
0
if(attachment.sizes){ 
    if( attachment.sizes.thumbnail !== undefined ) url_image=attachment.sizes.thumbnail.url; 
    else if(attachment.sizes.medium !== undefined) url_image=attachment.sizes.medium.url; 
    else url_image=attachment.sizes.full.url; 

    console.log( url_image ); 
    jQuery('<li data-id="'+attachment.id+'"><img src="'+url_image+'"></li>').appendTo(blocImages); 
} 
1

你可以循環中的對象attachment,像這樣的attachment.sizes子對象:

custom_file_frame.on('select', function() { 
    var attachment = custom_file_frame.state().get('selection').first().toJSON(); 

    /* The Loop here */ 

    $.each(attachment.sizes, function(name, attribs) { 
     console.log(name); 
     $.each(attribs, function(attrib, value) { 
      console.log("\t" + attrib + " ==> " + value); 
     }); 
    }); 


}); 

所以,你可以檢索自定義圖像尺寸太大。

;-)

相關問題