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

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

{dede:sql sql="select * from dede_uploads where arcid = ~id~"}
[field:global.cfg_basehost/][field:url/]
{/dede:sql}

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

QQ截图20180421085201

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

QQ截图20180421085430

 

所以这种当然 不行了.

解决方法 :

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

这个是DEDEHTML大神提供的.在这里表示感谢!

THE END