简体   繁体   中英

Programmatically add featured image with ALT text in Wordpress?

There are examples of how to add post with featured image programmatically in wordpress, but I need to add post with featured image and ALT text too. How can I do something like that?

Thank you.

If you look at the source for the wp_get_attachment_image function, you can see how the alt text is being fetched:

'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ))

From there it is easy enough to figure out how to update the alt text.

update_post_meta($attachment_id, '_wp_attachment_image_alt', 'My Alt Text');

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