WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the advanced custom fields plugin to upload images for an image gallery. I am attempting to retrieve the different sizes of images that are saved in the uploads folder and having problems.

$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');

When I echo out the results of this I get nothing. I know that the images are being saved as different sizes because I can see them in the uploads folder and the get_sub_field('image') is returning the proper image src.

Is there anything else I need to do to get this to work?

share|improve this question
up vote 1 down vote accepted

What's in get_sub_field('image')? wp_get_attachment_image_src() parameter 1 should be the attachment id, not the attachment src, if that's what you have.

I use the following function to get an attachment ID from an image url as as far as I know WP doesn't have a method for this at present.

function get_attachment_id_from_src ($src) {
    global $wpdb;

    $reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";

    $src1 = preg_replace($reg,'',$src);

    if($src1 != $src){
    $ext = pathinfo($src, PATHINFO_EXTENSION);
    $src = $src1 . '.' .$ext;

$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
$id = $wpdb->get_var($query);

return $id;


share|improve this answer
ah ok.. thank you. That is a very nice little function to have. – pfunc Nov 27 '11 at 23:08

Your Answer


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.