Support

Account

Forum Replies Created

  • Nevermind found a fix myself 🙂

    I think the main reason was related to the fact that the “return format” was set to “Image URL” instead of “Image Array”.

    After I changed that in my WordPress backend I now use the following code in my template and it gives me the alt tag and title tag of the image:

    <?php 
    $image = get_sub_field('inside_the_customer_imgs'); 
    printf( '<img src="%s" alt="%s" title="%s" />', $image['url'], $image['alt'], $image['title'] );         
    ?>
  • 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 🙂

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