POST TIME:2021-05-23 02:58
織夢DEDECMS內容頁獲取圖片URL地址的方法
{dede:sql sql="select * from dede_uploads where arcid = ~id~"}
[field:global.cfg_basehost/][field:url/]
{/dede:sql}
這個方法是適用于內容只有一張圖片,效果如下:
(此圖片來源于網絡,如有侵權,請聯系刪除! )
如果是有多張圖片 ,獲取的就是所有圖片的地址
(此圖片來源于網絡,如有侵權,請聯系刪除! )
所以這種當然 不行了.
解決方法 :
打開 /include/common.func.php 文件。
在最后面添加下面:
function firstimg($str_pic)
{
$str_sub=str_replace("-lp","","$str_pic"); //把縮略圖中的‘"-lp"’ 刪掉就是第一張圖的地址了。
return $str_sub;
}
然后在模板里循環調用:
[field:litpic function=firstimg(‘@me’)/]
直接調用方法:
{dede:field name=’litpic’ function=”firstimg(@me)”/}
運用顯示:
<a title=”[field:title/]” href=”[field:arcurl/]”><img alt=”[field:title/]” src=”[field:litpic function=firstimg(‘@me’)/]”/></a>
還有一種不用修改核心文件的方法 ,就是下面的代碼.
{dede:field.body runphp=yes}preg_match_all("/<img([^>]*)s*src=('|")([^'"]+)('|")/",@me,$matches);$imgsrc_arr = array_unique($matches[3]);@me = $imgsrc_arr[0];{/dede:field.body}