织梦侠
WWW.2L3.NET

dedecms织梦tag keyword调用相关文章的代码

DEDE官方的dede:likearticle是利用文章中输入的关键词或者自动分词形成的关键词来关联文章,但是有时候自动生成的关键词并不精确,造成关联不准,这里我们通过自定义一个类库文件来实现精准获取相关文章
可以单用tag来调用相关文章,也可用tag+keyword来调用相关文章(默认tag优先于keyword,可以自己排序)
 
include/taglib 下添加一个liketags.lib.php 代码如下
  1. <?php
  2. if(!defined('DEDEINC')) exit('Request Error!');
  3. function lib_liketags(&$ctag,&$refObj)
  4. {
  5.     global $dsql;
  6.      
  7.     //属性处理
  8.     $attlist="row|12,titlelen|30,infolen|250,col|1,tablewidth|100,mytypeid|0,byabs|0,imgwidth|120,imgheight|90,flag|";
  9.     FillAttsDefault($ctag->CAttribute->Items,$attlist);
  10.     extract($ctag->CAttribute->Items, EXTR_SKIP);
  11.     $revalue = '';
  12.      
  13.     if(empty($tablewidth)) $tablewidth = 100;
  14.     if(empty($col)) $col = 1;
  15.     $colWidth = ceil(100/$col);
  16.     $tablewidth = $tablewidth."%";
  17.     $colWidth = $colWidth."%";
  18. $orwheres = '';
  19. if($flag != '') {
  20. $flags = explode(',', $flag);
  21. for($i=0; isset($flags[$i]); $i++) $orwheres .= " AND FIND_IN_SET('{$flags[$i]}', arc.flag)>0 ";
  22. }
  23.  
  24.     $ids = array();
  25.     $tids = array();
  26.      
  27.     if(!empty($refObj->Fields['tags'])) {
  28.         $keyword = $refObj->Fields['tags'];
  29.     }
  30.     else {
  31.         $keyword = ( !empty($refObj->Fields['keywords']) ? $refObj->Fields['keywords'] : '' );
  32.     }
  33.      
  34.     $typeid = ( !empty($mytypeid) ? $mytypeid : 0 );
  35.     if(empty($typeid))
  36.     {
  37.         if(!empty($refObj->Typelink->TypeInfos['reid'])) {
  38.              $typeid = $refObj->Typelink->TypeInfos['reid'];
  39.         }
  40.         else {
  41.              if(!empty($refObj->Fields['typeid'])) $typeid = $refObj->Fields['typeid'];
  42.         }
  43.     }
  44.      
  45.     if( !empty($typeid) && !preg_match('#,#', $typeid) ) {
  46.         $typeid = GetSonIds($typeid);
  47.     }
  48.      
  49. if(!empty($refObj->Fields['tags']) && $eregtype != 'keyword' )
  50. {
  51. $tags = explode(',', addslashes($refObj->Fields['tags']));
  52. $getsql = " tag like '".join("' OR tag like '", $tags)."' ";
  53. $dsql->Execute('me', "Select * From `dede_tagindex` where $getsql ");
  54. while($arow = $dsql->GetArray('me'))
  55. {
  56. $tids[] = $arow['id'];
  57. }
  58. $tid = join(',', $tids);
  59. if($tid!='')
  60. {
  61. $dsql->Execute("me", "Select aid From `dede_taglist` where tid in($tid) And arcrank > -1 group by aid order by aid desc");
  62. while($arow = $dsql->GetArray("me"))
  63. {
  64. $ids[] = $arow['aid'];
  65. $arcid =  $refObj->Fields['aid'];
  66. }
  67. }
  68. }
  69. if($eregtype == 'tag' && count($ids) == 0 )
  70. {
  71. return;
  72. }
  73. else
  74. {
  75. if(count($ids) > 0)
  76. {
  77. if(!empty($typeid))
  78. {
  79. $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  80. }
  81. $idsStr = join(',', $ids);
  82. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  83. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  84. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  85. where arc.arcrank>-1 and arc.id in($idsStr) $typeid order by arc.id desc limit 0, $row";
  86. }
  87. else
  88. {
  89. $limitRow = $row - count($ids);
  90. $keyword = '';
  91. if(!empty($refObj->Fields['keywords']))
  92. {
  93. $keywords = explode(',' , trim($refObj->Fields['keywords']));
  94. $keyword = '';
  95. $n = 1;
  96. foreach($keywords as $k)
  97. {
  98. if($n > 3)  break;
  99.  
  100. if(trim($k)=='') continue;
  101. else $k = addslashes($k);
  102.  
  103. $keyword .= ($keyword=='' ? " CONCAT(arc.keywords,' ',arc.title) like '%$k%' " : " OR CONCAT(arc.keywords,' ',arc.title) like '%$k%' ");
  104. $n++;
  105. }
  106. }
  107. $arcid = (!empty($refObj->Fields['id']) ? $refObj->Fields['aid'] : 0);
  108. if( empty($arcid) || $byabs==0 )
  109. {
  110. $orderquery = " order by arc.id desc ";
  111. }
  112. else
  113. {
  114. $orderquery = " order by ABS(arc.id - ".$arcid.") ";
  115. }
  116. if($keyword != '')
  117. {
  118. if(!empty($typeid))
  119. {
  120. $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  121. }
  122. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  123. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  124. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  125. where arc.arcrank>-1 and ($keyword)  $typeid $orderquery limit 0, $row";
  126. }
  127. else
  128. {
  129. if(!empty($typeid))
  130. {
  131. $typeid = " arc.typeid in($typeid) And arc.id<>$arcid ";
  132. }
  133. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  134. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  135. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  136. where arc.arcrank>-1 and  $typeid $orderquery limit 0, $row";
  137. }
  138. }
  139. }
  140.  
  141.     $innertext = trim( $ctag->GetInnerText() );
  142.     if($innertext=='') $innertext = GetSysTemplets('part_arclist.htm');
  143.     $dsql->SetQuery($query);
  144.     $dsql->Execute('al');
  145.     $artlist = '';
  146.     if($col > 1){
  147.         $artlist = "<table width='$tablewidth' border='0' cellspacing='0' cellpadding='0'>rn";
  148.     }
  149.     $dtp2 = new DedeTagParse();
  150.     $dtp2->SetNameSpace('field', '[', ']');
  151.     $dtp2->LoadString($innertext);
  152.     $GLOBALS['autoindex'] = 0;
  153.     $line = $row;
  154.     for($i=0; $i < $line; $i++)
  155.     {
  156.         if($col>1) $artlist .= "<tr>rn";
  157.         for($j=0; $j < $col; $j++)
  158.         {
  159.             if($col>1) $artlist .= "    <td width='$colWidth'>rn";
  160.             if($row = $dsql->GetArray("al"))
  161.             {
  162.                 $ids[] = $row['id'];
  163.                 //处理一些特殊字段
  164.                 $row['info'] = $row['infos'] = cn_substr($row['description'],$infolen);
  165.                 $row['id'] =  $row['id'];
  166.  
  167.                 if($row['corank'] > 0 && $row['arcrank']==0)
  168.                 {
  169.                     $row['arcrank'] = $row['corank'];
  170.                 }
  171.  
  172.                 $row['filename'] = $row['arcurl'] = GetFileUrl($row['id'],$row['typeid'],$row['senddate'],$row['title'],$row['ismake'],
  173.                 $row['arcrank'],$row['namerule'],$row['typedir'],$row['money'],$row['filename'],$row['moresite'],$row['siteurl'],$row['sitepath']);
  174.  
  175.                 $row['typeurl'] = GetTypeUrl($row['typeid'],$row['typedir'],$row['isdefault'],$row['defaultname'],$row['ispart'],
  176.                 $row['namerule2'],$row['moresite'],$row['siteurl'],$row['sitepath']);
  177.  
  178.                 if($row['litpic'] == '-' || $row['litpic'] == '')
  179.                 {
  180.                     $row['litpic'] = $GLOBALS['cfg_cmspath'].'/images/defaultpic.gif';
  181.                 }
  182.                 if(!preg_match("#^http://#i", $row['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y')
  183.                 {
  184.                     $row['litpic'] = $GLOBALS['cfg_mainsite'].$row['litpic'];
  185.                 }
  186.                 $row['picname'] = $row['litpic'];
  187.                 $row['stime'] = GetDateMK($row['pubdate']);
  188.                 $row['typelink'] = "<a href='".$row['typeurl']."'>".$row['typename']."</a>";
  189.                 $row['image'] = "<img src='".$row['picname']."' border='0' width='$imgwidth' height='$imgheight' alt='".preg_replace("#['><]#","",$row['title'])."'>";
  190.                 $row['imglink'] = "<a href='".$row['filename']."'>".$row['image']."</a>";
  191.                 $row['fulltitle'] = $row['title'];
  192.                 $row['title'] = cn_substr($row['title'], $titlelen);
  193.                 if($row['color']!='') $row['title'] = "<font color='".$row['color']."'>".$row['title']."</font>";
  194.                 if(preg_match('#b#', $row['flag'])) $row['title'] = "<strong>".$row['title']."</strong>";
  195.                 $row['textlink'] = "<a href='".$row['filename']."'>".$row['title']."</a>";
  196.                 $row['plusurl'] = $row['phpurl'] = $GLOBALS['cfg_phpurl'];
  197.                 $row['memberurl'] = $GLOBALS['cfg_memberurl'];
  198.                 $row['templeturl'] = $GLOBALS['cfg_templeturl'];
  199.                  
  200.                 if(is_array($dtp2->CTags))
  201.                 {
  202.                     foreach($dtp2->CTags as $k=>$ctag)
  203.                     {
  204.                         if($ctag->GetName()=='array') {
  205.                             $dtp2->Assign($k,$row);
  206.                         }
  207.                         else {
  208.                             if(isset($row[$ctag->GetName()])) $dtp2->Assign($k,$row[$ctag->GetName()]);
  209.                             else $dtp2->Assign($k,'');
  210.                         }
  211.                     }
  212.                     $GLOBALS['autoindex']++;
  213.                 }
  214.  
  215.                 $artlist .= $dtp2->GetResult()."rn";
  216.             }
  217.             //if hasRow
  218.             else
  219.             {
  220.                 $artlist .= '';
  221.             }
  222.             if($col>1) $artlist .= "    </td>rn";
  223.         }
  224.         //Loop Col
  225.         if($col>1) $i += $col - 1;
  226.         if($col>1) $artlist .= "    </tr>rn";
  227.     }
  228.     //loop line
  229.     if($col>1) $artlist .= "    </table>rn";
  230.     $dsql->FreeResult("al");
  231.     return $artlist;
  232. }
前台模板调用
  1. {dede:liketags row='3' mytypeid='6' eregtype='all|tag|keyword' titlelen='250' infolen='250'}
  2. <li>
  3. <a href="[field:arcurl/]" title="[field:title/]" target="_blank">
  4. <div class="list-img">
  5. <img src="[field:litpic/]" alt="[field:title/]"/>
  6. </div>
  7. <div class="list-info">
  8. <p class="title">[field:title/]</p>
  9. <p class="meta">[field:description /]</p>
  10. </div>
  11. </a>
  12. </li>
  13. {/dede:liketags}
转载请注明文章来自织梦侠[秩名]作者的-dedecms织梦tag keyword调用相关文章的代码
联系作者
取消
这个作者很懒什么也没留下!
打赏作者
取消

本文作者无偿奉献,就打赏给我们织梦侠吧!

扫码支付
扫码打赏,建议金额1-10元

打开支付宝扫一扫,即可进行扫码打赏哦

提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。