Support

Account

Home Forums ACF PRO Image ID & Alt Tag

Solved

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:

    <?php 
    $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.

    Thanks.

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

    <?php 
    
    $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?

    Thanks

  • 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: https://www.advancedcustomfields.com/resources/image

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

You must be logged in to reply to this topic.