zblog php图片自动添加alt属性教程

zblog教程 3243

alt是图片标签img的一个属性,其作用除了在图像无法显示时,浏览器把alt属性值替代图片显示外,在SEO优化中也是非常重要的一个标签属性,时下的SEO检测工具都把alt列为重要检测指标。

在使用zblog php编辑器上传插入图片过程中,博主往往会忽略或者漏掉图片alt,事后想起再逐一检查显然极度消耗时间,而通过函数代码则可以自动给所有图片添加alt属性,一劳永逸。

操作步骤

1、编辑主题的include.php文件,在最后一行的?>上面添加下面的函数代码:

1
2
3
4
5
6
7
8
9
function imgAlt(&$template){
	global $zbp;
	$article = $template->GetTags('article');
	$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>/i";
	$replacement = '<img alt="'.$article->Title.'" src=$2$3.$4$5/>';
	$content = preg_replace($pattern, $replacement, $article->Content);
	$article->Content = $content;
	$template->SetTags('article', $article);
}

2、在include.php文件的ActivePlugin_xxx()函数部分,添加挂载接口代码:

1
Add_Filter_Plugin('Filter_Plugin_ViewPost_Template','imgAlt');

3、保存后,文章中的图片就会自动添加alt属性,并把文章标题设置为alt属性值。

代码来自应用中心的Tctitleseo插件,不想动手的博主,可以直接在应用中心安装插件实现。

精品推荐: