Support

Account

Home Forums Add-ons Flexible Content Field Alt text using the_subfield not showing Reply To: Alt text using the_subfield not showing

  • Hi Jonathan and thanks for your quick reply.

    I have set the image field to ID. But after your reply I checked the code again and found the error.

    I had used the_sub_field instead of get_sub_field.

    So thanks for validating that your solution also worked on sub fields that pointed me in the right direction.

    I saw your answer in this topic (http://support.advancedcustomfields.com/forums/topic/how-to-use-alt-tags/) and used the last solution you provided.

    Final code that worked for me.

    Code added to function.php

    
    // Alt text for ACF get_sub_field images
    
    function get_image_with_alt($imagefield, $postID, $imagesize = 'full'){
    $imageID = get_sub_field($imagefield, $postID); 
    $image = wp_get_attachment_image_src( $imageID, $imagesize ); 
    $alt_text = get_post_meta($imageID , '_wp_attachment_image_alt', true); 
    return '<img src="' . $image[0] . '" alt="' . $alt_text . '" />';
    }
    
    

    And then all it like this where you want the image in your template:

    
    
    <?php echo get_image_with_alt('NAME OF THE IMAGE FIELD', get_the_ID(), ''); ?>