今天奇它论坛@老白在网上冲浪的时候发现,WordPress网站的图片alt和title标签属性也有利于seo排名优化,这不赶紧盘起来!操作简单,免代码即可实现wordpress文章标题自动链接图片alt和title标签属性,往往我们很多人在编辑文章的时候,都不注重文章标签的填写或者是懒得写,那这样就导致文章内图片缺少ALT 标签!
以下是从技术宅那里找到的方法,有两种(原文:https://jszbug.com/18081)
标题+站点名
function img_alt($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', 'img_alt', 99999);
标题链接图片alt和title标签属性
//图片alt// function img_alt($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().'" title="'.get_the_title().'"', $images[0][$index]); $content = str_replace($images[0][$index], $new_img, $content); } } return $content; } add_filter('the_content', 'img_alt', 99999); //图片alt//
将以上代码选择其一复制到主题的
functions.php
中即可