Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wonder how I can link manually to the smallest filesize for the_post_thumbnail() within its filter function …

add_filter( 'post_thumbnail_html', 'post_thumbnail_html', 10, 3 );

function post_thumbnail_html( $html, $post_id, $post_image_id ) {

    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . '<img class="digest" src="' . $post_image_id . '" alt="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '" />' . '</a>';

    return $html;

}

So now my image src is just the post_id (e.g. 38). How can I link to the smallest filesize possible and add another data-attribute that links to the largest file?

My output should finally look like this … 

<img class="digest" src="small-post-thumbnail.jpg" data-fullsrc="full-post-thumbnail.jpg" alt="something" />

Thank you in advance!

edit/update: I'd love to do the same for images inside the_content(). Any idea how to apply the same thing to normal images in posts. The src should always hold the smallest size wordpress creates and the data attribute should link to the largest filesize.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Since you already know your $post_image_id, you can use wp_get_attachment_src()

$small = wp_get_attachment_src($post_image_id, 'smallest-size');
$large = wp_get_attachment_src($post_image_id, 'full');

$smallURL = $small[0];
$largeURL = $large[0];

where 'smallest-size' is the name of the image-size you have predefined in add_image_size()

share|improve this answer
    
Thank you, this works fine! Any ideas on how I could apply the same thing to normal images inside the_content()? –  mathiregister May 29 '12 at 12:08
    
Like @janw said, i think that'll be tough. I've seen people do it with some regex to filter the images markup from the_content filter and re-processing them to fit. –  moraleida May 29 '12 at 12:22
    
Check this answer just posted -> wordpress.stackexchange.com/a/53597/7890 –  moraleida May 29 '12 at 12:34

post_thumbnail_html has 5 arguments not 3 like you use, in arg #5 are the attributes.
The thumbs should already be the smallest file.

http://adambrown.info/p/wp_hooks/hook/post_thumbnail_html?version=3.3&file=wp-includes/post-thumbnail-template.php

Applying this to the_content will be much harder

share|improve this answer

Your Answer

 
discard

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.