0

Get the attachment ID from the file URL

// retrieves the attachment ID from the file URL
function c2_get_image_id($image_url) {
 global $wpdb;
 $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url )); 
        return $attachment[0]; 
}

Call function

// set the image url
$image_url = 'http://yoursite.com/wp-content/uploads/2012/02/14/image_name.jpg';
  // store the image ID in a var
$image_id = c2_get_image_id($image_url);
  // retrieve the thumbnail size of our image
$image_thumb = wp_get_attachment_image_src($image_id, 'thumbnail');
  // display the image
echo $image_thumb[0];

Jagdish Sarma Asked question February 17, 2023
Add a Comment