找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1157|回复: 0

[分享] dedecms相关文章标签匹配tag、关键词、标题及调用全站相关文章方

[复制链接]
发表于 2018-7-24 20:27:58 | 显示全部楼层 |阅读模式 来自 中国–河南–新乡
接触dedecms是从2009开始的,以前一直用风讯和科讯(ASP的)这两款,后来从成都到东莞进一公司打工,该公司就是使用的dedecms建站,我也跟着学了,怎么说呢刚开始觉得很强大,到后面用着越来越糟糕,很多地方做的不完善,另外一个让大家很火的地方就是,在dede的论坛提问题基本上官方是不会给你解答的。想调用相关文章,以tag优先调用,如果tag匹配不到或者匹配条数不够再以关键词匹配。
dedecms的相关文章标签(likearticle.lib.php)
新版的dedecms5.7相关文章标签有两个问题
1、相关文章文章调只调用当前栏目的,其他栏目的调用不到
2、只能匹配其他文章的标题(title),和关键词(keywords),没有去匹配其他文章tag
修改dedecms(5.7)likearticle.lib.php相关文章标签调用全站相关文章
打开likearticle.lib.php(请事先备份),
查找:$query = "
在其上方添加:$typeid = ' AND arc.id<>$arcid '; 注意有两处,AND arc.id<>$arcid这个的意思就是限制调用出来的相关文章不包含本文章,记的dedecms7.0之前没有做 这个限制-_-,这样就取消了相关文章匹配栏目的限制了。
dedecms相关文章调用tag、关键词(keywords)、标题(title)相同的文章
dedecms官方的相关文章标签并没有匹配tag相同的文章,他只是获取当前文章的tag和关键词(keywords)去匹配其他文章的标题(title)、关键词(keywords) 请看dede的源码片断
  1. if(!empty($refObj->Fields['keywords']))
  2. {
  3. $keywords = explode(',' , trim($refObj->Fields['keywords']));
  4. $keyword = '';
  5. $n = 1;
  6. foreach($keywords as $k)
  7. {
  8. if($n > 3) break;

  9. if(trim($k)=='') continue;
  10. else $k = addslashes($k);

  11. $keyword .= ($keyword=='' ? " CONCAT(arc.keywords,' ',arc.title) LIKE '%$k%' " : " OR CONCAT(arc.keywords,' ',arc.title) LIKE '%$k%' ");
  12. $n++;
  13. }
  14. }
  15. $arcid = (!empty($refObj->Fields['id']) ? $refObj->Fields['aid'] : 0);
  16. if( empty($arcid) || $byabs==0 )
  17. {
  18. $orderquery = " ORDER BY arc.id desc ";
  19. }
  20. else {
  21. $orderquery = " ORDER BY ABS(arc.id - ".$arcid.") ";
  22. }
  23. if($keyword != '')
  24. {
  25. if(!empty($typeid)) {
  26. $typeid = " AND arc.typeid IN($typeid) AND arc.id<>$arcid ";
  27. }
  28. $query = "SELECT arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  29. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  30. FROM `dede_archives` arc LEFT JOIN `dede_arctype` tp ON arc.typeid=tp.id
  31. where arc.arcrank>-1 AND ($keyword) $typeid $orderquery limit 0, $row";
  32. }
  33. else
  34. {
  35. if(!empty($typeid)) {
  36. $typeid = " arc.typeid IN($typeid) AND arc.id<>$arcid ";
  37. }
  38. $query = "SELECT arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  39. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  40. FROM `dede_archives` arc LEFT JOIN `dede_arctype` tp ON arc.typeid=tp.id
  41. WHERE arc.arcrank>-1 AND $typeid $orderquery limit 0, $row";
  42. }
复制代码

注意以上绿色内容就是一部分匹配条件 ,$keyword .= ($keyword=='' ? " CONCAT(arc.keywords,' ',arc.title) LIKE '%$k%' " : " OR CONCAT(arc.keywords,' ',arc.title) LIKE '%$k%' ");可以看出dedecms只CONCAT了keywords和title,没有做tag的处理,这里提供一个方法原创作者不详。

  1. #p#副标题#e#<?php
  2. if (!defined('DEDEINC'))
  3. exit('Request Error!');
  4. function lib_likearticle(&$ctag, &$refObj)
  5. {
  6. global $dsql;

  7. $attlist = "row|12,titlelen|28,infolen|150,col|1,tablewidth|100,mytypeid|0,byabs|0,imgwidth|120,imgheight|90";
  8. FillAttsDefault($ctag->CAttribute->Items, $attlist);
  9. extract($ctag->CAttribute->Items, EXTR_SKIP);
  10. $revalue = '';

  11. if (empty($tablewidth))
  12. $tablewidth = 100;
  13. if (empty($col))
  14. $col = 1;
  15. $colWidth = ceil(100 / $col);
  16. $tablewidth = $tablewidth . "%";
  17. $colWidth = $colWidth . "%";

  18. $ids = array();
  19. $tids = array();
  20. $arcid = $refObj->Fields['aid'];

  21. /* if (empty($typeid)) {
  22. if (!empty($refObj->Typelink->TypeInfos['reid'])) {

  23. $typeid = $refObj->Typelink->TypeInfos['reid'];
  24. } else {
  25. if (!empty($refObj->Fields['typeid']))
  26. $typeid = $refObj->Fields['typeid'];
  27. }
  28. }

  29. if (!empty($typeid) && !ereg(',', $typeid)) {
  30. $typeid = GetSonIds($typeid);
  31. }*/

  32. if (!empty($refObj->Fields['tags']) && $eregtype != 'keyword') {
  33. $tags = explode(',', addslashes($refObj->Fields['tags']));

  34. $getsql = " tag like '" . join("' OR tag like '", $tags) . "' ";

  35. $dsql->Execute('me', "Select * From `dede_tagindex` where $getsql ");
  36. while ($arow = $dsql->GetArray('me')) {
  37. $tids[] = $arow['id'];
  38. }
  39. $tid = join(',', $tids);
  40. if ($tid != '') {
  41. $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");
  42. while ($arow = $dsql->GetArray("me")) {
  43. $ids[] = $arow['aid'];
  44. $arcid = $refObj->Fields['aid'];
  45. }
  46. }
  47. }
  48. $typeid = (!empty($mytypeid) ? $mytypeid : ' And arc.id<>'.$arcid.' ');
  49. if ($eregtype == 'tag' && count($ids) == 0) {
  50. return '';
  51. } else {
  52. if (count($ids) > 0) {
  53. /* if (!empty($typeid)) {
  54. $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  55. }*/
  56. $idsStr = join(',', $ids);
  57. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  58. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  59. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  60. where arc.id in($idsStr) $typeid order by arc.id desc";
  61. }else{

  62. $limitRow = $row - count($ids);
  63. $keyword = '';
  64. if (!empty($refObj->Fields['keywords'])) {
  65. $keywords = explode(',', trim($refObj->Fields['keywords']));
  66. $keyword = '';
  67. $n = 1;
  68. foreach ($keywords as $k) {
  69. if ($n > 3)
  70. break;

  71. if (trim($k) == '')
  72. continue;
  73. else
  74. $k = addslashes($k);

  75. $keyword .= ($keyword == '' ? " CONCAT(arc.keywords,' ',arc.title) like '%$k%' " : " OR CONCAT(arc.keywords,' ',arc.title) like '%$k%' ");
  76. $n++;
  77. }
  78. }
  79. $arcid = (!empty($refObj->Fields['id']) ? $refObj->Fields['aid'] : 0);
  80. if (empty($arcid) || $byabs == 0) {
  81. $orderquery = " order by arc.id desc ";
  82. } else {
  83. $orderquery = " order by ABS(arc.id - " . $arcid . ") ";
  84. }
  85. if ($keyword != '') {
  86. /* if (!empty($typeid)) {
  87. $typeid = " And arc.typeid in($typeid) And arc.id<>$arcid ";
  88. }*/
  89. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  90. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  91. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  92. where arc.arcrank>-1 and ($keyword) $typeid $orderquery limit 0, $row";
  93. } else {
  94. /* if (!empty($typeid)) {
  95. $typeid = " arc.typeid in($typeid) And arc.id<>$arcid ";
  96. }*/
  97. $query = "Select arc.*,tp.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
  98. tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
  99. from `dede_archives` arc left join `dede_arctype` tp on arc.typeid=tp.id
  100. where arc.arcrank>-1 and $typeid $orderquery limit 0, $row";
  101. }
  102. }

  103. }
  104. $innertext = trim($ctag->GetInnerText());
  105. if ($innertext == '')
  106. $innertext = GetSysTemplets('part_arclist.htm');
  107. $dsql->SetQuery($query);
  108. $dsql->Execute('al');
  109. $artlist = '';
  110. if ($col > 1) {
  111. $artlist = "<table width='$tablewidth' border='0' cellspacing='0' cellpadding='0'>rn";
  112. }
  113. $dtp2 = new DedeTagParse();
  114. $dtp2->SetNameSpace('field', '[', ']');
  115. $dtp2->LoadString($innertext);
  116. $GLOBALS['autoindex'] = 0;
  117. $line = $row;
  118. for ($i = 0; $i < $line; $i++) {
  119. if ($col > 1)
  120. $artlist .= "<tr>rn";
  121. for ($j = 0; $j < $col; $j++) {

  122. if ($col > 1)
  123. $artlist .= " <td width='$colWidth'>rn";
  124. if ($row = $dsql->GetArray("al")) {
  125. $ids[] = $row['id'];
  126. $row['info'] = $row['infos'] = cn_substr($row['description'], $infolen);
  127. $row['id'] = $row['id'];
  128. if ($row['corank'] > 0 && $row['arcrank'] == 0) {
  129. $row['arcrank'] = $row['corank'];
  130. }
  131. $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']);
  132. $row['typeurl'] = GetTypeUrl($row['typeid'], $row['typedir'], $row['isdefault'], $row['defaultname'], $row['ispart'], $row['namerule2'], $row['moresite'], $row['siteurl'], $row['sitepath']);
  133. if ($row['litpic'] == '-' || $row['litpic'] == '') {
  134. $row['litpic'] = $GLOBALS['cfg_cmspath'] . '/images/defaultpic.gif';
  135. }
  136. if (!eregi("^", $row['litpic']) && $GLOBALS['cfg_multi_site'] == 'Y') {
  137. $row['litpic'] = $GLOBALS['cfg_mainsite'] . $row['litpic'];
  138. }
  139. $row['picname'] = $row['litpic'];
  140. $row['stime'] = GetDateMK($row['pubdate']);
  141. $row['typelink'] = "<a href='" . $row['typeurl'] . "'>" . $row['typename'] . "</a>";
  142. $row['image'] = "<img src='" . $row['picname'] . "' border='0' width='$imgwidth' height='$imgheight' alt='" . ereg_replace("['><]", "", $row['title']) . "'>";
  143. $row['imglink'] = "<a href='" . $row['filename'] . "'>" . $row['image'] . "</a>";
  144. $row['fulltitle'] = $row['title'];
  145. $row['title'] = cn_substr($row['title'], $titlelen);
  146. if ($row['color'] != '')
  147. $row['title'] = "<font color='" . $row['color'] . "'>" . $row['title'] . "</font>";
  148. if (ereg('b', $row['flag']))
  149. $row['title'] = "<strong>" . $row['title'] . "</strong>";
  150. $row['textlink'] = "<a href='" . $row['filename'] . "'>" . $row['title'] . "</a>";
  151. $row['plusurl'] = $row['phpurl'] = $GLOBALS['cfg_phpurl'];
  152. $row['memberurl'] = $GLOBALS['cfg_memberurl'];
  153. $row['templeturl'] = $GLOBALS['cfg_templeturl'];

  154. if (is_array($dtp2->CTags)) {
  155. foreach ($dtp2->CTags as $k => $ctag) {
  156. if ($ctag->GetName() == 'array') {
  157. $dtp2->Assign($k, $row);
  158. } else {
  159. if (isset($row[$ctag->GetName()]))
  160. $dtp2->Assign($k, $row[$ctag->GetName()]);
  161. else
  162. $dtp2->Assign($k, '');
  163. }
  164. }
  165. $GLOBALS['autoindex']++;
  166. }
  167. $artlist .= $dtp2->GetResult() . "rn";
  168. } else {
  169. $artlist .= '';
  170. }
  171. if ($col > 1)
  172. $artlist .= " </td>rn";
  173. }
  174. if ($col > 1)
  175. $i += $col - 1;
  176. if ($col > 1)
  177. $artlist .= " </tr>rn";
  178. }
  179. if ($col > 1)
  180. $artlist .= " </table>rn";
  181. $dsql->FreeResult("al");
  182. return $artlist;
  183. }
  184. ?>
复制代码

用以上内容替换likearticle.lib.php的所有内容,这样调用出来的相关文章就相对完美了些,之所以说相对原因是修改后调用相关文章是优先匹配tag,如果tag能匹配到哪怕是匹配到一条就不会再去匹配关键词了,比如我想调用10条,该篇文章tag能匹配到一条,但是关键词能匹配20条,调用出来的效果也只有一条,我现在的做法是用两个标签tag和关键词分别调用5条,这样出来的效果稍微好些。标签调用代码如下:
  1. {dede:likearticle row='5' col=1 titlelen='54' eregtype=tag}
  2. <li><a href="[field:arcurl/]" title="[field:fulltitle/]">[field:title/]</a></li>
  3. {/dede:likearticle}
  4. {dede:likearticle row='5' col=1 titlelen='54' eregtype=keyword}
  5. <li><a href="[field:arcurl/]" title="[field:fulltitle/]">[field:title/]</a></li>
  6. {/dede:likearticle}
复制代码

不得不说
dedecms还是算不错的了,新手入门速度快,标签简单易懂,我做了好几个站都是用dedecms做的,最后希望dedecms越做越好,越来越完善吧!

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-11-23 10:38 , Processed in 0.044095 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表