首页 > 网站 > WordPress图片自动加alt和title标签

前因

之前做bing网站扫描的时候,扫出个这个玩意

WordPress图片自动加alt和title标签插图

缺少了图像的Alt属性?这是什么?有什么用?

但是作为仅次于被robots.txt阻止的SEO问题,还是值得重视的。

食用

代码解决

我们可以在function.php加入以下代码,即可解决

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

插件解决

Smart SEO Tool插件自带,可以尝试使用这个插件。

相关文章