WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It is a bug?

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

Same code, called in template, and in AJAX call returns same image SRC, but different image width and height.

dumb from template call:

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

dump from AJAX call

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

i'm confused, whats wrong?

index.php code

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

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

    <?php endwhile; endif; ?>

post.php code

<div class="container">

if( in_array( $post_type, array( 'audio', 'video', 'quote', 'link'))) {
  $theme->theme_post->display_post_element( $post_type, $post_size, $post);

display_post_element function code

    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>

load posts with ajax call code:

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;

share|improve this question
You added 'post-thumb-size-small' as additional image size? Btw: Please update your Q with some more info about your ajax setup (code), etc. – kaiser Feb 19 '12 at 22:10
@kaiser, of course i added this image size... I'll try to update the question in more details – user12859 Feb 19 '12 at 22:21
Sometimes WP doesn't resize images properly and instead just puts width and height attributes on the <img/> element to force it to the size you wanted. Could your ajax be getting the "actual" image size instead? – Dominic Feb 19 '12 at 23:00
If you added a new image size to your theme old images don't have the new image size yet. If that's the case it will return a different size. You can regenerate your thumbnails with a plugin like Regenerate thumbnails. – Rob Vermeer Feb 19 '12 at 23:34
@Dominic +1 for the idea. Anyway, WP is getting the attributes from $hwstring = image_hwstring($width, $height), which is nothing than formatting the outcoming of wp_get_attachment_image_src(). – kaiser Feb 19 '12 at 23:39

Handle built in sizes right

I wrote a class that handles all the different stuff for built in image sizes.

  1. To redefine the built in sizes, use an extended class (the upper file in the linked source)
  2. To unset a class and skip it's useage completely, just set w & h to 0.

Click the octocat to view the gist :)

share|improve this answer
+1+ for the octocat link :))) . . . . & of course, for the fine piece of code - Dynamic Image Resize, aka Timthumb Done Right, is bloody cool as well! – brasofilo Jul 4 '12 at 12:29
@brasofilo Thanks :) – kaiser Jul 4 '12 at 12:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.