2012-02-21 171 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')); 
相关问题