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.


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()

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


Applying this to the_content will be much harder

| improve this answer | |

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