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

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

概述DedeCMS默认自带的相关文章是likearticle标签实现,实际应用的时候发现这个标签调用的相关文章并不完美,我们需要改进下,一般我们要调用相关文章都是以Tag或者keyword匹配。

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。内存溢出小编现在分享给大家,也给大家做个参考。

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

<?PHP

if(!defined('DEDEINC')) exit('Request Error!');

/*--------------------------------------

按关键词关连文章的标签

eregtype属性

eregtype = keyword 只匹配关键字

eregtype = tag 只匹配tag

eregtype = all tag优先,不存在tag则匹配关键字

mytypeID='' 栏目ID,多个栏目用,隔开

---------------------------------------*/

function lib_likearticle(&$ctag,&$refObj)

{

global $dsql;

 

//属性处理

$attList="row|12,Titlelen|28,infolen|150,col|1,tablewIDth|100,mytypeID|0,byabs|0,imgwIDth|120,imgheight|90";

FillAttsDefault($ctag->CAttribute->Items,$attList);

extract($ctag->CAttribute->Items,EXTR_SKIP);

$revalue = '';

 

if(empty($tablewIDth)) $tablewIDth = 100;

if(empty($col)) $col = 1;

$colWIDth = ceil(100/$col);

$tablewIDth = $tablewIDth."%";

$colWIDth = $colWIDth."%";

 

$IDs = array();

$tIDs = array();

 

$typeID = ( !empty($mytypeID) ? $mytypeID : 0 );

if(empty($typeID))

{

if(!empty($refObj->Typelink->TypeInfos['reID'])) {

$typeID = $refObj->Typelink->TypeInfos['reID'];

}

else {

if(!empty($refObj->FIElds['typeID'])) $typeID = $refObj->FIElds['typeID'];

}

}

 

if( !empty($typeID) && !ereg(',',$typeID) ) {

$typeID = GetSonIDs($typeID);

}

if(!empty($refObj->FIElds['Tags']) && $eregtype != 'keyword' )

{

$Tags = explode(',addslashes($refObj->FIElds['Tags']));

 

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

 

$dsql->Execute('me',"Select * From `dede_tagindex` where $getsql ");

while($arow = $dsql->GetArray('me')) {

$tIDs[] = $arow['ID'];

}

$tID = join(',$tIDs);

if($tID!='')

{

$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");

while($arow = $dsql->GetArray("me"))

{

$IDs[] = $arow['aID'];

$arcID = $refObj->FIElds['aID'];

}

}

}

 

if($eregtype == 'tag' && count($IDs) == 0 )

{

return '';

}

else

{

if(count($IDs) > 0)

{

if(!empty($typeID)) {

$typeID = " And arc.typeID in($typeID) And arc.ID<>$arcID ";

}

$IDsstr = join(',$IDs);

$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

from `dede_archives` arc left join `dede_arctype` tp on arc.typeID=tp.ID

where arc.ID in($IDsstr) $typeID order by arc.ID desc";

}

else

{

$limitRow = $row - count($IDs);

$keyword = '';

if(!empty($refObj->FIElds['keywords']))

{

$keywords = explode(',trim($refObj->FIElds['keywords']));

$keyword = '';

$n = 1;

foreach($keywords as $k)

{

if($n > 3) break;

 

if(trim($k)=='') continue;

else $k = addslashes($k);

 

$keyword .= ($keyword=='' ? " CONCAT(arc.keywords,' ',arc.Title) like '%$k%' " : " OR CONCAT(arc.keywords,arc.Title) like '%$k%' ");

$n++;

}

}

$arcID = (!empty($refObj->FIElds['ID']) ? $refObj->FIElds['aID'] : 0);

if( empty($arcID) || $byabs==0 ) {

$orderquery = " order by arc.ID desc ";

}

else {

$orderquery = " order by ABS(arc.ID - ".$arcID.") ";

}

if($keyword != '')

{

if(!empty($typeID)) {

$typeID = " And arc.typeID in($typeID) And arc.ID<>$arcID ";

}

$query = "Select arc.*,tp.sitepath

from `dede_archives` arc left join `dede_arctype` tp on arc.typeID=tp.ID

where arc.arcrank>-1 and ($keyword) $typeID $orderquery limit 0,$row";

}

else

{

if(!empty($typeID)) {

$typeID = " arc.typeID in($typeID) And arc.ID<>$arcID ";

}

$query = "Select arc.*,tp.sitepath

from `dede_archives` arc left join `dede_arctype` tp on arc.typeID=tp.ID

where arc.arcrank>-1 and $typeID $orderquery limit 0,$row";

}

}

}

$innertext = trim( $ctag->GetInnerText() );

if($innertext=='') $innertext = GetSystemplets('part_arcList.htm');

$dsql->Setquery($query);

$dsql->Execute('al');

$artList = '';

if($col > 1) {

$artList = "<table wIDth='$tablewIDth' border='0' cellspacing='0' cellpadding='0'> ";

}

$dtp2 = new DedeTagParse();

$dtp2->SetnameSpace('fIEld','[',']');

$dtp2->LoadString($innertext);

$GLOBALS['autoindex'] = 0;

$line = $row;

for($i=0; $i < $line; $i++)

{

if($col>1) $artList .= "<tr> ";

for($j=0; $j < $col; $j++)

{

if($col>1) $artList .= " <td wIDth='$colWIDth'> ";

if($row = $dsql->GetArray("al"))

{

$IDs[] = $row['ID'];

//处理一些特殊字段

$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['isdefault'],$row['defaultname'],$row['ispart'],

$row['namerule2'],$row['sitepath']);

if($row['litpic'] == '-' || $row['litpic'] == '')

{

$row['litpic'] = $GLOBALS['cfg_CMSpath'].'/images/defaultpic.gif';

}

if(!eregi("^http://",$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'>";

$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(ereg('b',$row['flag'])) $row['Title'] = "<strong>".$row['Title']."</strong>";

$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'];

 

if(is_array($dtp2->CTags))

{

foreach($dtp2->CTags as $k=>$ctag)

{

if($ctag->Getname()=='array') {

$dtp2->Assign($k,$row);

}

else {

if(isset($row[$ctag->Getname()])) $dtp2->Assign($k,$row[$ctag->Getname()]);

else $dtp2->Assign($k,'');

}

}

$GLOBALS['autoindex']++;

}

$artList .= $dtp2->GetResult()." ";

}

//if hasRow

else

{

$artList .= '';

}

if($col>1) $artList .= " </td> ";

}

//Loop Col

if($col>1) $i += $col - 1;

if($col>1) $artList .= " </tr> ";

}

//loop line

if($col>1) $artList .= " </table> ";

$dsql->FreeResult("al");

return $artList;

}

?>

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

总结

以上是内存溢出为你收集整理的DedeCMS通过Tag和Keyword标签获取相关文章方法全部内容,希望文章能够帮你解决DedeCMS通过Tag和Keyword标签获取相关文章方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zz/1026123.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存