Home Forums ACF PRO Image ID & Alt Tag


Image ID & Alt Tag

  • Hi,

    Just wondering if anyone can help, please. I’m trying to get the alt tag for my image. I currently have the current code:

    $attachment_id_desktop = get_sub_field('image');
    $size_desktop = "column-size"; // (thumbnail, medium, large, full or custom size)
    $image_desktop = wp_get_attachment_image_src( $attachment_id_desktop, $size_desktop );
    $alt_desktop = get_post_meta($attachment_id_desktop , '_wp_attachment_image_alt', true);
    <img class="img-fluid desktop" alt="" src="<?php echo $image_desktop[0]; ?>" alt="<?php echo $alt_desktop; ?>"/>

    The image works, but the Alt tag is not being displayed.


  • Make sure that your Image field has the Return Format set to Image Array, then use the following code as an example:

    $image = get_field('image');
    if( !empty( $image ) ): ?>
        <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo 
    esc_attr($image['alt']); ?>" />
    <?php endif; ?>
  • Hi,

    Thanks for that. I’m using the ID, instead of array so that I can size my image.

    Can I do that with Array?


  • Turns out I was being completely stupid and had do alt tags, you can see above. That’s why it wasn’t working. Removed one and it worked.

  • This is exactly what the Image Array was designed for. You’re essentially doing it all manually when you don’t need to.

    Look at the docs:

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Image ID & Alt Tag’ is closed to new replies.