简体   繁体   中英

Getting image alt from WordPress. get_post_thumbnail_id() not working

In WPBakery, I've mapped out a module which requires an image:

public function image_html($atts) {
    extract(
        shortcode_atts(
            array(
                'image' => '',
            ),
            $atts
        )
    );

    $getImage = shortcode_atts(
        array(
            'image'  =>  'image',
        ),
        $atts
    );

    $image_ids = explode(',',$getImage['image']);
    foreach($image_ids as $image_id) {
        $images = wp_get_attachment_image_src($image_id, 'full');
        $image = $images[0];
        $thumb_id = get_post_thumbnail_id(get_the_ID()); 
        $alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
    }
}

I've used wp_get_attachment_image_src to get the attached image alt text, but it doesn't seem to work. And yes, the image does have alt text in WordPress > Media > Alternative Text.

The markup is as follows:

<img src="<?php echo $image; ?>" alt="<?php echo $alt; ?>" />

$image appears fine, $alt shows up empty.

Try with alternative way:

$thumb_id = get_post_meta( get_the_ID(), '_thumbnail_id', true );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM