Support

Account

Home Forums Front-end Issues can no longer get ALT + Title for images

Solved

can no longer get ALT + Title for images

  • Something changed in a recent update of ACF that has broken some functionality that had worked for years. The following code was used for generating both responsive images plus their ALT and TITLE tags, which is essential for good SEO.

    Hope someone can help me figure out what changed, as I don’t see anything in the recent changelogs that would seem to apply to this. (Well, in the eyes of a VERY junior PHP user!)

    <?php
    $image = get_sub_field('photo') ;
    $size = '2048x2048';
    $alt = $image['alt'];
    $title = $image['title'];
    ;?>

    <?php if( $image ) {
    echo wp_get_attachment_image( $image, $size, false, array('alt'=>$alt,'title'=>$title));
    };
    ?>

  • Here’s the error it kicks back:

    Warning: Trying to access array offset on value of type int in...

    If I remove:

    $alt = $image['alt'];
    $title = $image['title'];

    Then everything works… but I don’t get those tags written in of course.

  • After banging away on various solutions, I finally guessed that simply rewriting the array could solve it. Also found some better methods for getting the ALT and TITLE values.

    Here’s my code for responsive SEO images with ALT and TITLE tags!

    <?php
    	$image = get_sub_field('photo') ;
    	$size = '2048x2048'; 
    	$alt = get_post_meta($image, '_wp_attachment_image_alt', TRUE);
    	$img_title = get_the_title($image);						
    ;?>
    
     <?php if( $image ) {
    		echo wp_get_attachment_image( $image, $size, array('alt'=>$alt), array('title'=>$img_title));
    	};
    ?>
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.