2012-02-21 169 views
2

這是一個錯誤?wp_get_attachment_image返回不同​​的圖像大小

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

相同的代碼,在模板中調用,並在AJAX調用返回相同的圖像SRC,但不同的圖像寬度和高度。從模板調用

轉儲:從AJAX

<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"> 

轉儲調用

<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功能代碼

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

負荷職位與Ajax調用代碼:

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圖片的網址。

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

(你可以得到get_posts()附件ID(一個或多個)。)

然後,您知道該網址,圖像中$image[0]。所以我所做的就是把在正確的尺寸:

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

它的效果並不理想,但至少它的工作原理。它可能在WP 3.5中修復,但我不知道,我仍在使用3.4.2。

+0

這仍然是WP 4.0中的一個問題。我需要正確的尺寸,不能通過圖像去顯示圖像。你有沒有找到其他的解決方法?謝謝 – Alvaro 2014-11-05 16:58:57

0

是不是editor_max_image_sizeimage_constrain_size_for_editor()的末尾media.php?已知與wp_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')); 
相關問題