首页  »  站长资讯  »  网站优化

织梦DEDECMS内容页获取图片url地址的方法

2023/03/11 12:18     创新屋收录网     已浏览282次

织梦DEDECMS内容页获取图片URL地址的方法


{dede:sql sql="select * from dede_uploads where arcid = ~id~"}


[field:global.cfg_basehost/][field:url/]


{/dede:sql}


这个方法是适用于内容只有一张图片,效果如下:


织梦DEDECMS内容页获取图片url地址的方法



如果是有多张图片 ,获取的就是所有图片的地址



织梦DEDECMS内容页获取图片url地址的方法



所以这种当然 不行了.



解决方法 :



打开 /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)”/}



运用显示:



”[field:title/]” 



还有一种不用修改核心文件的方法 ,就是下面的代码.



{dede:field.body runphp=yes}preg_match_all("/]*)s*src=('|")([^'"]+)('|")/",@me,$matches);$imgsrc_arr = array_unique($matches[3]);@me = $imgsrc_arr[0];{/dede:field.body}