简体   繁体   English

在Wordpress中以编程方式添加带有ALT文本的特色图片?

[英]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. 有一些示例如何在wordpress中以编程方式添加带有特色图片的帖子,但我也需要添加带有特色图片和ALT文本的帖子。 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: 如果查看wp_get_attachment_image函数的源代码,则可以看到如何获取替代文本:

'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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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