Support

Account

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

  • I tried the function you created @jonathan, this is how I used to display the image:

    <img src="<?php the_sub_field('inside_the_customer_imgs'); ?>">

    This is you function, to which I also added the title field:

    function get_image_with_alt_and_title ($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);
    	$title_text = get_the_title($imageID);
    	
    	return '<img src="' . $image[0] . '" alt="' . $alt_text . '" title="' . $title_text . '" />';
    }

    And this is how I call the function in my template:

    <?php echo get_image_with_alt_and_title('inside_the_customer_imgs', get_the_ID(), 'thumbnail'); ?>

    but somehow when I run this on my page I get the following output:

    <img src="" alt="" title="THE TITLE OF THE PAGE WHERE THE IMAGE IS ON">

    If I change the get_field to get_sub_field in the function the output becomes completely empty:

    <img src="" alt="" title="">

    Any idea what I could be doing wrong here? I know it has been ages since this request originally came, but Google led me here 🙂