WordPress.org

Forums

[resolved] How to display ALT text of featured image? (8 posts)

  1. Jole
    Member
    Posted 1 year ago #

    I managed to show title, caption and description of featured image in post with:

    echo get_post(get_post_thumbnail_id())->post_title;
    echo get_post(get_post_thumbnail_id())->post_excerpt;
    echo get_post(get_post_thumbnail_id())->post_content;

    But, how to show image alt ("Alt Text") of featured image?

    Best regards.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    $thumbnail = get_post( get_post_thumbnail_id() );
    echo $thumbnail->post_title;
    echo $thumbnail->post_excerpt;
    echo $thumbnail->post_content;
    echo get_post_meta( $thumbnail->ID, '_wp_attachment_image_alt', true );
  3. Jole
    Member
    Posted 1 year ago #

    Thank you, it works.

    I found similar solution just before your answer like this:

    $thumb_img = get_post_meta( get_post_thumbnail_id() ); // Get post ID
    echo $thumb_img['_wp_attachment_image_alt']['0']; echo '<br />';// show alt text

    I need more help :-)

    How to display image file name, with or without extension (bare image name), format?

    I asked for this here:

  4. Jole
    Member
    Posted 1 year ago #

  5. Jole
    Member
    Posted 1 year ago #

    Also, can you make another function to check if there is alt text at all, and if there is to show it, and if there is no alt text to echo "some other text"?

  6. keesiemeijer
    moderator
    Posted 1 year ago #

    Try this for the alt text

    $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';
    }
  7. Jole
    Member
    Posted 1 year ago #

    Thank you, but now in both cases (with or without alt) I get "no alt text found"?

    Am I doing something wrong?

  8. alirao
    Member
    Posted 2 months ago #

    i would love if you and your readers can read my article on “adding alternative Text, Hyperlinks, giving photo-credits” i would love to see your feedback. and Yeh it also contains a video i uploaded, just for better understanding :)

    Visit my blog and do give your feedback
    http://www.thenextrex.com/alternative-text-or-alt-text-in-wordpress/

Topic Closed

This topic has been closed to new replies.

About this Topic