WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Jole
    Member
    Posted 8 months 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 8 months 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 8 months 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 8 months ago #

  5. Jole
    Member
    Posted 8 months 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 8 months 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 8 months ago #

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

    Am I doing something wrong?

Reply

You must log in to post.

About this Topic