2012-02-21 5 views
2

これはバグですか?wp_get_attachment_imageが異なる画像サイズを返す

wp_get_attachment_image($attachment_id, 'post-thumb-size-small'); 

テンプレート内で呼び出され、AJAX呼び出しで呼び出される同じコードが同じ画像SRCを返しますが、画像の幅と高さは異なります。

テンプレート呼び出しからダンプ:

<img width="286" height="189" src="http://localhost/site/files/2012/02/post-image-31-286x189.jpg" class="attachment-post-thumb-size-small" alt="post-image-3" title="post-image-3"> 

ダンプAJAX呼び出し

<img width="220" height="145" src="http://localhost/site/files/2012/02/post-image-31-286x189.jpg" class="attachment-post-thumb-size-small" alt="post-image-3" title="post-image-3"> 

からいただきました間違った私は、混乱していますか?

index.phpをコード

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<?php include 'post.php'; ?> 

<?php endwhile; endif; ?> 

post.phpコード

<div class="container"> 

<?php 

    $theme->theme_post->display_post_element($post_type, $post_size, $post); 

?> 
</div> 

display_post_element機能コードAJAX呼び出しコードで

function display_post_element($post_type, $post_size, $post) { 
$attachment_id = get_post_meta($post->ID, '_view_attachment_id', true); 
     if($post_type == 'single_image') { 
      $img = wp_get_attachment_image_src($attachment_id, 'full'); 

      if(is_array($img)):     
      ?> 
      <div class="preview-thumb"> 
       <a href="<?php echo $img[0]; ?>" class="lightbox"><?php echo wp_get_attachment_image($attachment_id, 'post-thumb-size-' . $post_size); ?></a> 
       <a href="<?php echo $img[0]; ?>" class="lightbox zoom"></a> 
      </div> 
      <?php 
      endif; 
     } 
} 

負荷ポスト:

function load_posts_ajax() { 
    global $post; 
    $query_string = $_POST['query_string']; 

    query_posts($query_string . '&posts_per_page=' . get_option('posts_per_page') . '&post_status=publish&offset=' . (int)$_POST[ 'off']); 

    if (have_posts()) : while (have_posts()) : the_post(); 
     include TEMPLATEPATH . '/post.php'; 
    endwhile; endif; 

    die; 
} 

テーマコンストラクタのfunctions.phpで画像サイズを定義しました。 私はget_intermediate_image_sizes()をダンプし、すべての画像サイズはAJAX呼び出しでロードされます

答えて

0

おかげさま...あなたは配列でサイズを設定しようとしましたか?

wp_get_attachment_image($attachment_id, array(220,145)); // for 220x145 
0

これは実際にはバグです。私は同じ問題に遭遇しました。私たちは同様の状況を抱えていたので、AJAX呼び出しと何か関係があります。なぜ、それが間違った次元を返すのか知っています。 しかし、、私は回避策を考え出しましたが、これはかなり古い質問ですが、他の誰かがこの問題に遭遇するかもしれないと思いました。

私のソリューションは、このでした:

まず、あなたはwp_get_attachment_image_srcで画像のURLを取得します。

$image = wp_get_attachment_image_src($attachment_id, 'image size name');

(あなたがget_posts()と添付ファイルID(複数可)を取得することができます。)

その後、$image[0]に画像のURLを持っています。それでは、私がやったことは正しい寸法に入れた:

<img src="'.$image[0].'" width="960" height="300" />

それは理想的ではないですが、少なくともそれが動作します。それはWP 3.5で修正されるかもしれませんが、私はまだわかりません、私はまだ3.4.2を使用しています。

+0

これはWP 4.0ではまだ問題です。私は正しいサイズが必要で、イメージで画像を表示できません。他の回避策を見つけましたか?ありがとう – Alvaro

0

image_constrain_size_for_editor()の末尾にeditor_max_image_sizeがありません。media.phpwp_get_attachment_image()の出力を混乱させることが知られています。

function bypass_editor_max_image_size(){ 
    if(!empty($width) && !empty($height)){ 
     return array($width, $height); 
    }else{ 
     return; 
    } 
} 

その後、画像srcとサイズを取得するためにこの呼び出しを追加/削除します。

add_filter('editor_max_image_size', array($this, 'bypass_editor_max_image_size')); 
$image = wp_get_attachment_image_src($photo, $size); 
remove_filter('editor_max_image_size', array($this, 'bypass_editor_max_image_size')); 
関連する問題