DedeCMS织梦

DedeCMS通过Tag和Keyword标签获取相关文章方法

阿里云

DedeCMS 默认自带的相关文章是 likearticle 标签实现,实际应用的时候发现这个标签调用的相关文章并不完美,我们需要改进下,一般我们要调用相关文章都是以 Tag 或者 keyword 匹配,我们今天就做下改进,实现全站按 TAG 和关键字调用相关文章,这个方法可单用 tag 来调用相关文章,也可用 tag+keyword 来调用相关文章(默认 tag 优先于 keyword,可以自己排序),非常适合 SEO,经测试效果极佳。具体方法如下,把下面的 php 代码换掉 include/taglib/likearticle.lib 里的代码:

  1. <?php
  2. if(!defined('DEDEINC')) exit('Request Error!');
  3. /*--------------------------------------
  4. 按关键词关连文章的标签
  5. eregtype属性
  6. eregtype = keyword 只匹配关键字
  7. eregtype = tag 只匹配tag
  8. eregtype = all tag优先,不存在tag则匹配关键字
  9. mytypeid='' 栏目ID,多个栏目用,隔开
  10. ---------------------------------------*/
  11. function lib_likearticle(&$ctag,&$refObj)
  12. {
  13.   global $dsql;
  14.  
  15.   //属性处理
  16.   $attlist="row|12,titlelen|28,infolen|150,col|1,tablewidth|100,mytypeid|0,byabs|0,imgwidth|120,imgheight|90";
  17.   FillAttsDefault($ctag->CAttribute->Items,$attlist);
  18.   extract($ctag->CAttribute->Items, EXTR_SKIP);
  19.   $revalue = '';
  20.  
  21.   if(empty($tablewidth)) $tablewidth = 100;
  22.   if(empty($col)) $col = 1;
  23.   $colWidth = ceil(100/$col);
  24.   $tablewidth = $tablewidth."%";
  25.   $colWidth = $colWidth."%";
  26.  
  27.   $ids = array();
  28.   $tids = array();
  29.  
  30.   $typeid = ( !empty($mytypeid) ? $mytypeid : 0 );
  31.   if(empty($typeid))
  32.   {
  33.       if(!empty($refObj->Typelink->TypeInfos['reid'])) {
  34.           $typeid = $refObj->Typelink->TypeInfos['reid'];
  35.       }
  36.       else {
  37.           if(!empty($refObj->Fields['typeid'])) $typeid = $refObj->Fields['typeid'];
  38.       }
  39.   }
  40.  
  41.   if( !empty($typeid) && !ereg(',', $typeid) ) {
  42.       $typeid = GetSonIds($typeid);
  43.   }
  44.   if(!empty($refObj->Fields['tags']) && $eregtype != 'keyword' )
  45.   {
  46.     $tags = explode(',', addslashes($refObj->Fields['tags']));
  47.  
  48.     $getsql = " tag like '".join("' OR tag like '", $tags)."' ";
  49.  
  50.     $dsql->Execute('me', "Select * From `dede_tagindex` where $getsql ");
  51.     while($arow = $dsql->GetArray('me')) {
  52.       $tids[] = $arow['id'];
  53.     }
  54.     $tid = join(',', $tids);
  55.     if($tid!='')
  56.     {
  57.       $dsql->Execute("me", "Select aid From `dede_taglist` where tid in($tid) And arcrank > -1 group by aid order by aid desc limit 0, $row");
  58.       while($arow = $dsql->GetArray("me"))
  59.       {
  60.         $ids[] = $arow['aid'];
  61.                                                                 $arcid = $refObj->Fields['aid'];
  62.       }
  63.     }
  64.   }
  65.  
  66.   if($eregtype == 'tag' && count($ids) == 0 )
  67.   {
  68.     return '';
  69.   }
  70.   else
  71.   {
  72.     if(count($ids) > 0)
  73.     {
  74.                                   if(!empty($typeid)) {
  75.             $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  76.         }
  77.       $idsStr = join(',', $ids);
  78.       $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  79.                         tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  80.                         from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  81.                         where arc.id in($idsStr) $typeid order by arc.id desc";
  82.     }
  83.     else
  84.     {
  85.   $limitRow = $row - count($ids);
  86.   $keyword = '';
  87.   if(!empty($refObj->Fields['keywords']))
  88.   {
  89.       $keywords = explode(',' , trim($refObj->Fields['keywords']));
  90.       $keyword = '';
  91.       $n = 1;
  92.       foreach($keywords as $k)
  93.       {
  94.             if($n > 3) break;
  95.  
  96.             if(trim($k)=='') continue;
  97.             else $k = addslashes($k);
  98.  
  99.             $keyword .= ($keyword=='' ? " CONCAT(arc.keywords,' ',arc.title) like '%$k%' " : " OR CONCAT(arc.keywords,' ',arc.title) like '%$k%' ");
  100.               $n++;
  101.       }
  102.   }
  103.   $arcid = (!empty($refObj->Fields['id']) ? $refObj->Fields['aid'] : 0);
  104.   if( empty($arcid) || $byabs==0 ) {
  105.     $orderquery = " order by arc.id desc ";
  106.   }
  107.   else {
  108.     $orderquery = " order by ABS(arc.id - ".$arcid.") ";
  109.     }
  110.   if($keyword != '')
  111.   {
  112.         if(!empty($typeid)) {
  113.             $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  114.         }
  115.         $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  116.                         tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  117.                         from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  118.                         where arc.arcrank>-1 and ($keyword) $typeid $orderquery limit 0, $row";
  119.   }
  120.   else
  121.   {
  122.       if(!empty($typeid)) {
  123.           $typeid = " arc.typeid in($typeid) And arc.id<>$arcid ";
  124.       }
  125.       $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  126.                         tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  127.                         from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  128.                       where arc.arcrank>-1 and $typeid $orderquery limit 0, $row";
  129.                                       }
  130.                             }
  131.                       }
  132.   $innertext = trim( $ctag->GetInnerText() );
  133.   if($innertext=='') $innertext = GetSysTemplets('part_arclist.htm');
  134.   $dsql->SetQuery($query);
  135.   $dsql->Execute('al');
  136.     $artlist = '';
  137.   if($col > 1) {
  138.     $artlist = "<table width='$tablewidth' border='0' cellspacing='0' cellpadding='0'> ";
  139.   }
  140.   $dtp2 = new DedeTagParse();
  141.   $dtp2->SetNameSpace('field', '[', ']');
  142.   $dtp2->LoadString($innertext);
  143.   $GLOBALS['autoindex'] = 0;
  144.   $line = $row;
  145.   for($i=0; $i < $line; $i++)
  146.   {
  147.     if($col>1) $artlist .= "<tr> ";
  148.     for($j=0; $j < $col; $j++)
  149.     {
  150.       if($col>1) $artlist .= " <td width='$colWidth'> ";
  151.       if($row = $dsql->GetArray("al"))
  152.       {
  153.         $ids[] = $row['id'];
  154.         //处理一些特殊字段
  155.         $row['info'] = $row['infos'] = cn_substr($row['description'],$infolen);
  156.         $row['id'] = $row['id'];
  157.         if($row['corank'] > 0 && $row['arcrank']==0)
  158.         {
  159.             $row['arcrank'] = $row['corank'];
  160.         }
  161.         $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
  162.         $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
  163.         $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],$row['isdefault'],$row['defaultname'],$row['ispart'],
  164.         $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
  165.         if($row['litpic'] == '-' || $row['litpic'] == '')
  166.         {
  167.           $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
  168.         }
  169.         if(!eregi("^http://",$row['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y')
  170.         {
  171.           $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
  172.         }
  173.         $row['picname'] = $row['litpic'];
  174.         $row['stime'] = GetDateMK($row['pubdate']);
  175.         $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";
  176.         $row['image'] = "<img src='".$row['picname']."' border='0' width='$imgwidth' height='$imgheight'>";
  177.         $row['imglink'] = "<a href='".$row['filename']."'>".$row['image']."</a>";
  178.         $row['fulltitle'] = $row['title'];
  179.         $row['title'] = cn_substr($row['title'],$titlelen);
  180.         if($row['color']!='') $row['title'] = "<font color='".$row['color']."'>".$row['title']."</font>";
  181.         if(ereg('b',$row['flag'])) $row['title'] = "<strong>".$row['title']."</strong>";
  182.         $row['textlink'] = "<a href='".$row['filename']."'>".$row['title']."</a>";
  183.         $row['plusurl'] = $row['phpurl'] = $GLOBALS['cfg_phpurl'];
  184.         $row['memberurl'] = $GLOBALS['cfg_memberurl'];
  185.         $row['templeturl'] = $GLOBALS['cfg_templeturl'];
  186.  
  187.         if(is_array($dtp2->CTags))
  188.         {
  189.           foreach($dtp2->CTags as $k=>$ctag)
  190.           {
  191.             if($ctag->GetName()=='array') {
  192.               $dtp2->Assign($k,$row);
  193.             }
  194.             else {
  195.               if(isset($row[$ctag->GetName()])) $dtp2->Assign($k,$row[$ctag->GetName()]);
  196.               else $dtp2->Assign($k,'');
  197.             }
  198.           }
  199.           $GLOBALS['autoindex']++;
  200.         }
  201.         $artlist .= $dtp2->GetResult()." ";
  202.       }
  203.       //if hasRow
  204.       else
  205.       {
  206.         $artlist .= '';
  207.       }
  208.       if($col>1) $artlist .= " </td> ";
  209.     }
  210.     //Loop Col
  211.     if($col>1) $i += $col - 1;
  212.     if($col>1) $artlist .= " </tr> ";
  213.   }
  214.   //loop line
  215.   if($col>1) $artlist .= " </table> ";
  216.   $dsql->FreeResult("al");
  217.   return $artlist;
  218. }
  219. ?>
也想出现在这里?联系我们
创客主机

DedeCMS 通过 Tag 和 Keyword 标签获取相关文章方法

已有 445 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!