DedeCMS织梦内容页列表页调用自定义图片地址
 

DedeCMS有很多标签调用时自带了HTML代码,其实留给自己处理更好更灵活。比如自定义字段为图片时(字段名:name),输出的格式有时候并不是我们想要的,内容页用{dede:field.name/}
{dede:img text='' width='270' height='129'}/uploads/101017/1-10101H21F54P.gif{/dede:img} 前台输出格式如下:
<li> <a href='/uploads/140420/1-140420164Z2914.jpg' target='_blank'> <img src='/uploads/140420/1-140420164Z2914.jpg' width='253' border='0'/> </a></li>
创建自定义函数
打开/include/extend.func. php,在最下面复制以下代码(?>前面)
function GetOneImgUrl($img,$ftype=1){      if($img <> ''){          $dtp = new DedeTagParse();          $dtp->LoadSource($img);          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;          }  }  前台内容页格式为:
<img alt="{dede:field.title/}" src="{dede:field.name function='GetOneImgUrl(@me,1)'/}">@me,0  代表只输出路径@me,1  代表连宽、高一起输出同样的列表页或首页用相应的标签套进去就行了。

