WordPress能不安装WordPress插件就不安装,我们依然在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;
}