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="https://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="https://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">

<?php
if( in_array( $post_type, array( 'audio', 'video', 'quote', 'link'))) {
  $theme->theme_post->display_post_element( $post_type, $post_size, $post);
}
?>
</div>

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="https://wordpress.stackexchange.com/questions/42860/<?php echo $img[0]; ?>" class="lightbox"><?php echo wp_get_attachment_image( $attachment_id, 'post-thumb-size-' . $post_size); ?></a>
                <a href="https://wordpress.stackexchange.com/questions/42860/<?php echo $img[0]; ?>" class="lightbox zoom"></a>
            </div>
            <?php
            endif;
        }
}

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;

    die;
}

1 Answer
1

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.

http://static.steffenvogel.de/wp-content/uploads/2011/07/octocat_construction.gif

Click the octocat to view the gist 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *