織夢在PHP7.0環境中為文章圖片自動添加ALT屬性為標題方法,首先修改/include/extend.func.php,里面的把這段代碼加進去:
|
|
|
然后在我們需要調用織夢文章內容的地方加上調用代碼:
{dede:field.body function='replaceurl(@me)'/}
由于之前在《織夢如何去除img圖片中的style width height屬性》一文中,實現去高寬屬性,既然我們今天用自定義函數實現了替換alt屬性,那么也可以把去高寬屬性的這個功能加進去,完整代碼如下:
//文章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;
}
|

















