Warning: file_put_contents(pb_book.txt): failed to open stream: No space left on device in /www/wwwroot/wpcun.com/wp-content/themes/justnews/single.php on line 31

Warning: filemtime(): stat failed for pb_book.txt in /www/wwwroot/wpcun.com/wp-content/themes/justnews/single.php on line 36
WordPress教程:自动给图片添加alt标签 | 王牌文学网

WordPress教程:自动给图片添加alt标签

点击阅读全文

使用过百度站长评分的同学应该知道,图片如果木有alt标签会扣分的哦,当然,如果你平时有一个好的习惯,为图片自动添加alt标签的话,这样最好,那如果忘记了呢?不怕,懒人有懒人的办法。今天分享的这个方法来自大发。
我采用的是文章标题加站点名称的方式,虽然如果一片文章内图片较多的话不太标准,但是我大部分文章都是1张配图,于是用这个没什么压力。
图片优化也是wordpress seo的一种重要手段,不需要额外做什么,只需要复制粘贴下就可以达到效果,何乐而不为?
将以下代码直接放入functions.php即可:

/** Auto-Generate ALT tag for images */function image_alt_tag($content){    global $post;preg_match_all('/<img (.*?)/>/', $content, $images);    if(!is_null($images)) {        foreach($images[1] as $index => $value){            if(!preg_match('/alt=/', $value)){                $new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]);                $content = str_replace($images[0][$index], $new_img, $content);}        }    }    return $content;}add_filter('the_content', 'image_alt_tag', 99999);

 

                       
上一篇 2020年5月24日 16:17
下一篇 2020年5月24日 17:41

Warning: error_log(/www/wwwroot/wpcun.com/wp-content/plugins/spider-analyser/#log/log-2804.txt): failed to open stream: No space left on device in /www/wwwroot/wpcun.com/wp-content/plugins/spider-analyser/spider.class.php on line 2966