织梦园模板网提供云优cms模板,pbootcms模板,Wordpress主题模板等各类企业新闻资讯网站模板下载服务。织梦园网站模板交流群
用户名:
密码:
注册
注册后享受折扣价

织梦怎么让文章中图片自动添加ALT属性

织梦园模板 / 2021-04-21 / 收藏
打开/include/extend.func.php文件
在***下面加入:
function replaceurl($newurl)
{
global $dsql,$id;
//获取图片附加表imgurls字段内容进行处
$row = $dsql->GetOne("SELECT title FROM dede_archives where id=$id");
//替换图片Alt为文档标题
$newurl=str_ireplace(array('alt=""','alt=/'/''),'',$newurl);
$newurl=preg_replace("@ [/s]{0,}alt[/s]{0,}=[/"'/s]{0,}[/s/S]{0,}[/"'/s] @isU"," ",$newurl);
$newurl=str_ireplace("<img " ,"<img  alt=/"".$row['title']."/"",$newurl);
//去掉结尾空格
$newurl=str_ireplace(" /","/",$newurl);
$newurl=str_ireplace(" />","/>",$newurl);
return $newurl;
}

前端调用代码:
{dede:field.body function=’replaceurl(@me)’/}
需要图片中的style width height属性就把上面代码修改成下面的代码
//文章body优化替换
function replaceurl($newurl)
 {
global $dsql,$id;
//获取图片附加表imgurls字段内容进行处
$row = $dsql->GetOne("SELECT title FROM dede_archives where id=$id");
//去掉img的width和height
$newurl=preg_replace('/style=/"width/:(.*)/"/','',$newurl);
//替换图片Alt为文档标题
$newurl=str_ireplace(array('alt=""','alt=/'/''),'',$newurl);
$newurl=preg_replace("@ [/s]{0,}alt[/s]{0,}=[/"'/s]{0,}[/s/S]{0,}[/"'/s] @isU"," ",$newurl);
$newurl=str_ireplace("<img " ,"<img  alt=/"".$row['title']."/"",$newurl);
//去掉结尾空格
$newurl=str_ireplace(" /","/",$newurl);
$newurl=str_ireplace(" />","/>",$newurl);
return $newurl;
 }

相关安装使用

收缩