帝國CMS增加信息自動把內容圖片的alt和title屬性替換為信息標題
作者 :會員投稿 2020-03-04 11:05:09 審稿人 : haoz 圍觀 : 次 評論
網上有一些關于圖片添加ALT操作的教程了,墨魚今天完善一下,不光給圖片添加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.發布信息的時候就可以自動替換圖片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.發布信息的時候就可以自動替換圖片alt和title屬性為信息標題了!
標簽
上一篇:網站開啟評論引導用戶評論對網站SEO的影響有多大? 下一篇:返回列表
| 留言與評論(共有 0 條評論) |


