Grow your CSS skills. Land your dream job.

Retrieving the Attachment ID

  • # January 3, 2013 at 12:58 am

    I’m using the [ACF](http://www.advancedcustomfields.com/docs/field-types/image/ “”) plugin, and am trying to work out how to return the image ID – as in a unique integer associated with that image.

    I’m calling the URL like this:

    < ?php the_sub_field('background_photo'); ?>

    Does anyone happen to know what I have to do to return the unique ID?

    Thanks in advance.

    # January 3, 2013 at 1:33 am

    Maybe $attachment_id?

    # January 3, 2013 at 1:36 am

    @chrisburton How would I call that? I’ve tried this:

    < ?php the_sub_field('background_photo', $attachment_id); ?>

    This just returns the image URL again.

    # January 3, 2013 at 1:41 am

    My fault. Take a look at this: http://www.advancedcustomfields.com/docs/field-types/image/

    But specifically this:

    $image = get_field(‘image’);
    var_dump($image);

    # January 3, 2013 at 1:48 am

    @chrisburton I’m going to be honest. I have no clue what to do with that.

    I do know that that value is stored in id, as the array example they show after that line. But how to retrieve it I have no idea.

    # January 3, 2013 at 1:50 am

    What are you trying to do with this ID?

    # January 3, 2013 at 1:51 am

    This here:

    $image = get_sub_field(‘background_photo’);
    var_dump($image);

    Returns this:

    “string(109) “http://localhost:8888/bit-simple/wp-content/uploads/2013/01/4acae3924ef6d13cdc85f14979a2ffe0da15cb05-copy.jpg” “

    If I could just get the number inside that parentheses I’d be sorted!

    # January 3, 2013 at 1:52 am

    @chrisburton I’m trying to use it as a selector. I need to give each image a unique ID.

    # January 3, 2013 at 1:59 am

    $image = get_sub_field(‘background-photo’);
    echo $image( array(‘id’));

    My PHP is a bit rusty.

    # January 3, 2013 at 2:05 am

    @chrisburton That isn’t returning anything. :s

    # January 3, 2013 at 2:08 am

    Damn.

    $image = get_post( get_sub_field(‘background-photo’));
    $attachment = get_post_meta($image->ID, true);
    echo $attachment;

    # January 3, 2013 at 2:11 am

    It’s really late here so I have to get going. If that doesn’t work, perhaps someone else can give it a shot. Otherwise I’d contact ACF directly.

    # January 3, 2013 at 2:11 am

    @chrisburton Still nothing. Really appreciate the help and all, this has been killing me for hours. I don’t know what could be the problem.

    # January 3, 2013 at 10:48 am

    Bump. Anyone?

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".