WordPress优化之给文章图片添加alt和title属性
这是一条文章内容页公告,可在后台开启或关闭,此处可使用html标签...
在WordPress优化中。图片alt和图片title优化是很重要的一项seo优化,可是如果文章内图片数量过多,手动给每一张图片添加alt属性和图片title属性就会非常麻烦了,尤其是图片多的时候。
当然也有WordPress插件可以实现自动添加图片alt和图片title,但是小编是特别不喜欢用插件的,所以今天CINUI分享一篇免插件纯代码实现文章图片自动添加alt和title属性的WordPress教程。
方法很简单,将上面代码复制到主题的functions.php文件即可
//给文章图片自动添加alt和title信息
add_filter('the_content', 'imagesalt');
function imagesalt($content) {
global $post;
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
完成添加代码后,刷新文章看看图片的属性,是不是多出了alt和tilte属性呢?

UI掌控:cinui_
关注我们,获取更多的全网素材资源有趣有料!