dedecms模型添加的图片数据类型,在前台模板输出后是带有html代码的,我们在使用时只是需要图片地址就行了。
这样我们只需要写个自定义方法从字符串中把图片提取出来即可。
打开 \include\extend.func.php 在最下面添加
/**
* 自定义图片字段调用图片地址
*
* @access public
* @param string $fieldname 字段名
* @param string $ftype 输出样式
* @return string
*/
if(!function_exists('GetImgUrl'))
{
function GetImgUrl($fieldname, $ftype = 1)
{
if($fieldname != '')
{
$dtp = new DedeTagParse();
$dtp->LoadSource($fieldname);
if(is_array($dtp->CTags))
{
foreach($dtp->CTags as $ctag)
{
if($ctag->GetName() == 'img')
{
$width = $ctag->GetAtt('width');
$height = $ctag->GetAtt('height');
$imgurl = trim($ctag->GetInnerText());
$img = '';
if($imgurl != '')
{
if($ftype == 1)
{
$img .= $imgurl;
}
else
{
$img .= '<img src="' . $imgurl . '" width="' . $width . '" height="' . $height . '" />';
}
}
}
}
}
$dtp->Clear();
return $img;
}
}
}
前台模板使用
首页
{dede:arclist typeid=’1’ row=’14’ titlelen=’50’ channelid=’1’ addfields=’自定义字段名’}
[field:自定义字段名 function=GetImgUrl(@me)/]
{/dede:arclist}
列表页,自定义字段要勾选上这个才能在列表页使用
[field:自定义字段名 function=GetImgUrl(@me)/]
内容页
打开\templets\system\channel_article_image.htm 清空所有东西,只留下
[field:imgsrc/]
保存
这样就可以直接在内容页模板调用了
{dede:field.自定义字段名/}