wordpress自动给文章图片添加alt和title标签方法alt和title标签属性能给访客用户更好的浏览体验,同时对SEO也很有利,一般的主题都不具备自动添加标签的功能,而且我们在写文章的时候也没考虑那么多了,懒人的我们只能用代码来解决了:
文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html
自动用文章标题给文章图片添加alt和title标签属性文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html
在你主题目录下的函数文件 functions.php 中添加以下代码:文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html
//文章图片自动添加alt和title属性 function image_alt_tag($content){ global $post;preg_match_all('/<img (.*?)\/>/', $content, $images); if(!is_null($images)) {foreach($images[1] as $index => $value) {$new_img = str_replace('<img', '<img alt="'.get_the_title().'-'.get_bloginfo('name').'" title="'.get_the_title().'-'.get_bloginfo('name').'"', $images[0][$index]); $content = str_replace($images[0][$index], $new_img, $content);}} return $content; } add_filter('the_content', 'image_alt_tag', 99999);
如果文章没有这两个标签将自动获取文章标题添加。文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html
如果有安装缓存插件和使用CDN的站长刷新缓存就可以看到了。文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html 文章源自飞鸟学习网-https://www.pk88.net/2022-0226-7.html
全站资源源于网络整合,仅供学习研究,不可商用,不提供技术指导,如有侵权请告知删除,可以投稿,广告合作联系V: pk88net
我的微信
我的微信
微信扫一扫
评论