• Hi there,

    i tried to give out the alt text of a image. I only found the snippet

    <a href="<?php the_permalink(); ?>" alt="<?php echo $alt_text; ?>">

    But the variabl $alt_text; seems to be incorrectly, wordpress is telling me PHP Notice: Undefined variable: alt_text in …

    I hope someone knows the correct var ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Hi Deexgnome!

    I’m not entirely sure where you got that code from but yes, that code will give you that error. Where did you get the code from?

    You have to define the function first. I suppose you want to get alt of the featured image, then the function will be

    <?php $alt_text = get_post_meta( $thumbnail->ID, '_wp_attachment_image_alt', true );
    if( !empty( $alt_text )){
    	echo $alt_text;
    } else {
    	echo 'no alt text found';
    }
    ?>
    Thread Starter Theunknown1

    (@deexgnome)

    Hi ??

    @jose, i got it from Stackoverflow
    https://stackoverflow.com/questions/21945488/how-do-i-retrieve-and-display-the-alt-text-of-an-image-in-wordpress

    Thank you Local Fame, i try this and hit you back ??

    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Thanks!

    Taking a quick look one way I can think of would be to use something like:

    alt="<?php echo esc_attr( get_post_meta( get_the_ID(), '_wp_attachment_image_alt', true ) ); ?>" />

    My reasoning behind it is because get_post_meta needs to have a post ID passed to it and in the above example code you are passing to it an undefined object variable ( $thumbnail ) which could lead to another error.

    Let us know if that helps!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Giving out the alt Text of a Image’ is closed to new replies.