1

Wordpress Media Upload を使用して、サムネイル、サムネイル、ポスト サムネイルなどの特定の画像タイプを取得できないようです。

私が取得するのは画像の元のサイズの 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();

});

ありがとうございました :)

4

3 に答える 3

1

次のようattachment.sizesに、 object のサブオブジェクトをループできます。attachment

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);
        });
    });


});

したがって、カスタムの画像サイズも取得できます。

;-)

于 2017-02-21T13:33:11.033 に答える
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);
} 
于 2015-12-13T23:46:18.357 に答える