dedecms通过栏目关键字调用文章

2013-02-21 00:45 来源:www.chinab4c.com 作者:dedecms专家

     dedecms通过栏目关键字调用文章,就是通过dedecms的栏目设置的关键字,以及对该栏目下文章标题进行匹配,如果该文章标题含有该分类下面的关键字,就调用出来。   

  1:dedecms模板调用

  {dede:typekeys limit="3" titlelen="10"}
  <a href="[field:arcurl/]">[field:title/]<br></a>
  {/dede:typekeys}

 2:dedecms标签函数

 function lib_typekeys(&$ctag,&$refObj)

 global $dsql,$envs,$cfg_cmsurl;

 $keywords = $refObj->Fields['keywords'];
 $val      = explode(",",$keywords);
 if(empty($val)) return '';
 
 $tmp = '';
 foreach($val as $k){
  $tmp.=empty($tmp)? '':'or' ;
  $tmp.="  arc.title like '%".$k."%'";
 }
 if($tmp){
  $tmp =" and ($tmp)";
 }
 $list = "limit|5";
 FillAttsDefault($ctag->CAttribute->Items,$list);
 extract($ctag->CAttribute->Items);
 $tcp = new DedeTagParse();
 $tcp->SetNameSpace('field','[',']');
 $tcp->LoadSource($ctag->GetInnerText());
 
 $typeid = trim($ctag->GetAtt('typeid'));
    if(empty($typeid)) {
        $typeid = ( isset($refObj->Fields['typeid']) ? $refObj->Fields['typeid'] : $envs['typeid'] );
    }
 
 $orwheres = array();

    if(!empty($typeid) && $typeid != 'top')
 {
  //指定了多个栏目时,不再获取子类的id
  if( preg_match('#,#', $typeid) )
  {
   //指定了getall属性或主页模板例外
   if($getall==1 || empty($refObj->Fields['typeid']))
   {
    $typeids = explode(',', $typeid);
    foreach($typeids as $ttid) {
     $typeidss[] = GetSonIds($ttid);
    }
    $typeidStr = join(',', $typeidss);
    $typeidss = explode(',', $typeidStr);
    $typeidssok = array_unique($typeidss);
    $typeid = join(',', $typeidssok);
   }
   $orwheres[] = " arc.typeid IN ($typeid) ";
  }
  else
  {
   //处理交叉栏目
   $CrossID = '';
   if($ctag->GetAtt('cross')=='1')
   {
    $arr = $dsql->GetOne("SELECT `id`,`topid`,`cross`,`crossid`,`ispart`,`typename` FROM `dede_arctype` WHERE id='$typeid' ");
    if( $arr['cross']==0 || ( $arr['cross']==2 && trim($arr['crossid']=='') ) )
    {
     $orwheres[] = ' arc.typeid IN ('.GetSonIds($typeid).')';
     }
    else
    {
     $selquery = '';
     if($arr['cross']==1) {
      $selquery = "SELECT id,topid FROM `dede_arctype` WHERE typename LIKE '{$arr['typename']}' AND id<>'{$typeid}' AND topid<>'{$typeid}'  ";
     }
     else {
      $arr['crossid'] = preg_replace('#[^0-9,]#', '', trim($arr['crossid']));
      if($arr['crossid']!='') $selquery = "SELECT id,topid FROM `dede_arctype` WHERE id IN('{$arr['crossid']}') AND id<>'{$typeid}' AND topid<>'{$typeid}'  ";
     }
     if($selquery!='')
     {
      $dsql->SetQuery($selquery);
      $dsql->Execute();
      while($arr = $dsql->GetArray())
      {
       $CrossID .= ($CrossID=='' ? $arr['id'] : ','.$arr['id']);
      }
     }
    }
   }
   if($CrossID=='') $orwheres[] = ' arc.typeid IN ('.GetSonIds($typeid).')';
   else $orwheres[] = ' arc.typeid IN ('.GetSonIds($typeid).','.$CrossID.')';
  }
 }
 
 $orwhere = '';
    if(isset($orwheres[0])) {
        $orwhere = join(' And ',$orwheres);
  $orwhere = preg_replace("#^ And#is", '', $orwhere);
        $orwhere = preg_replace("#And[ ]{1,}And#is", 'And ', $orwhere);
    }
    if($orwhere!='') $orwhere = " WHERE $orwhere ";
 $query = "select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
        tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
        $addfieldsSql
        FROM `dede_archives` arc LEFT JOIN `dede_arctype` tp on arc.typeid=tp.id
        $addfieldsSqlJoin
        $orwhere $tmp $ordersql   $limitsql";
 $dsql->SetQuery($query);
 $dsql->Execute("a");
 while($row = $dsql->GetArray("a")){
  $row['info'] = $row['infos'] = cn_substr($row['description'],$infolen);
  $row['id'] =  $row['id'];

  if($row['corank'] > 0 && $row['arcrank']==0)
  {
   $row['arcrank'] = $row['corank'];
  }

  $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
  $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);

  $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],$row['isdefault'],$row['defaultname'],$row['ispart'],
  $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);

  if($row['litpic'] == '-' || $row['litpic'] == '')
  {
   $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
  }
  if(!preg_match("#^http:\/\/#i", $row['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y')
  {
   $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
  }
  $row['picname'] = $row['litpic'];
  $row['stime'] = GetDateMK($row['pubdate']);
  $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";
  $row['image'] = "<img src='".$row['picname']."' border='0' width='$imgwidth' height='$imgheight' alt='".preg_replace("#['><]#", "", $row['title'])."'>";
  $row['imglink'] = "<a href='".$row['filename']."'>".$row['image']."</a>";
  $row['fulltitle'] = $row['title'];
  $row['title'] = cn_substr($row['title'], $titlelen);
  if($row['color']!='') $row['title'] = "<font color='".$row['color']."'>".$row['title']."</font>";
  if(preg_match('#b#', $row['flag'])) $row['title'] = "<strong>".$row['title']."</strong>";
  //$row['title'] = "<b>".$row['title']."</b>";
  $row['textlink'] = "<a href='".$row['filename']."'>".$row['title']."</a>";

  $row['plusurl'] = $row['phpurl'] = $GLOBALS['cfg_phpurl'];
  $row['memberurl'] = $GLOBALS['cfg_memberurl'];
  $row['templeturl'] = $GLOBALS['cfg_templeturl'];
  
  foreach($tcp->CTags as $k=>$v){
   
   $tcp->Assign($k,$row[$v->GetName()]);
  }
  $str.=$tcp->GetResult();
  
 }
 
 return $str;

   
}
  以上就实现了dedecms通过栏目关键字调用文章.

 来源:http://www.chinab4c.com