帝国CMS发布文章信息时自动把图片的alt和title属性替换为信息标题

 2022-11-01    6901  

使用帝国CMS建站,想要在增加信息时自动把内容图片的alt和title属性替换为信息标题,具体怎么实现呢?下面一起来看看帝国CMS增加信息时自动把内容图片的alt和title属性替换为信息标题的操作方法。

操作方法如下:

1.把下面的代码放到/e/class/userfun.php (放到最后?>的前面)

function user_imgalt($mid,$f,$isadd,$isq,$value,$cs){
$title=$_POST['title'];
$htmls=$value;
$pattern = "/<img[^>]+>/"; 
preg_match_all($pattern, $htmls, $matches); 
for ($i=0; $i<=count($matches[0]); $i++) {
  preg_match_all("/alt=".+?"/",$matches[0][$i],$altimg); 
  preg_match_all("/title=".+?"/",$matches[0][$i],$titleimg); 
$t_alt=count($altimg[0]);
if($t_alt==0){
$htmls=str_replace("<img","<img alt="{$title}"",$htmls);
$htmls=str_replace("<img","<img title="{$title}"",$htmls);
}
}   
return $htmls;
}

2.在在后台管理数据表 管理字段 模型里正文字段一般是newstext(也可以是其它字段),在后台增加信息处理函数文本框填上user_imgalt

3.这样在帝国cms后台发布信息的时候就可以自动替换图片alt和title属性为信息标题了!


分享到:

本文链接:https://blog.lingruofeng.com/seo/39.html

版权声明:本站部分素材来自网络整理,如侵犯了您的合法权益,请联系我们删除!