Home Forums General Issues How to use ALT tags Reply To: How to use ALT tags

  • Hi,

    Yes you’d have to write out the code for all the places where you have images. If you have the images in a repeaterfield you do not however.. If you’re looking to create an gallery I’d suggest you take a look at the gallery field add on. With that you retrieve the alt text per default.

    If you prefer you could just create a function for this to simplify your code in the template. Put this in functions.php

    function get_image_with_alt($imagefield, $postID, $imagesize = 'full'){
    $imageID = get_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(), 'thumbnail'); ?>

    Note that the last parameter in the function is optional and it’s for if you want another image size than full.