Skip to content

Instantly share code, notes, and snippets.

@cfenzo
Created April 3, 2013 09:59
Show Gist options
  • Save cfenzo/5299919 to your computer and use it in GitHub Desktop.
Save cfenzo/5299919 to your computer and use it in GitHub Desktop.
How to replace timthumb with wordpress thumbnails for attachment-images in wordpress themes..
// add the thumbnail-sizes you need in functions.php
if ( function_exists( 'add_image_size' ) ) {
// repeat for all the thumbnail-sizes you need
add_image_size( 'thumb790x450', 790, 450, true );
}
// helper-function for getting the attachemnt-ID from source. You need this when you only have an image-src
function get_attachment_id_from_src ($image_src) {
global $wpdb;
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
$id = $wpdb->get_var($query);
return $id;
}
/* in any theme/plugin-file where timthumb is used */
// If you only have the url (here named $src), use get_attachment_id_from_src
$photoID = get_attachment_id_from_src($src);
// When you have the attachment-id (here named $photoID):
$thisthumb = wp_get_attachment_image_src( $photoID,"thumb790x450");
$thisthumb = $thisthumb[0];
echo '<img src="$thisthumb" />';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment