织梦自定义图片字段调用图片地址
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 .= '$imgurl . '" width="' . $width . '" height="' . $height . '" />'; } } } } } $dtp->Clear(); return $img; } } }
首页模板标签调用写法
{dede:arclist typeid='1' row='14' titlelen='50' channelid='模型id号' addfields='自定义图片字段名'}
[field:自定义图片字段名 function=GetImgUrl(@me)/]
{/dede:arclist}
模型id号怎么填?根据你自己自定义字段所在的内容模型来填 (如 channelid="1" 是 普通文章)
列表页模板标签调用写法
列表页dede:list标签写法,不需要channelid="模型id号" addfields="自定义字段名称"
这个只要在后台自定义字段里勾上使字段可以在列表的底层模板中获得 就行了
{dede:list pagesize='10' titlelen='250'}
[field:自定义图片字段名 function=GetImgUrl(@me)/]
{/dede:list}
内容页模板标签调用写法
打开\templets\system\channel_article_image.htm 清空所有东西,只留下一个
保存,这样就可以直接在内容页模板调用了
{dede:field.自定义图片字段名/}
原文链接: