织梦dedecms5.7全站怎么按tag与关键字调用相关文章?

发布时间:2019-10-04编辑:脚本学堂
本文介绍了织梦dedecms5.7全站按tag和关键字调用相关文章方法,可单用tag来调用相关文章,也可用tag+keyword来调用相关文章(默认tag优先于keyword,可以自己排序),需要的朋友参考下。

织梦dedecms5.7全站怎么按tag与关键字调用相关文章?

1,把以下代码保存到文章模版的适当位置
 

复制代码 代码示例:
<div class="xgwz">
<dl class="tbox">
<dt><strong>相关文章</strong></dt>
<dd>
<ul class="c1 ico2">
{dede:likearticle col='2' row='10' titlelen='42' eregtype=all|tag|keyword}
<li><a href="[field:arcurl/]">[field:title/]</a></li>
{/dede:likearticle}
</ul>
</dd>
</dl>
</div>

2,把以下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 jbxue_tagindex where $getsql ");
while($arow = $dsql->getarray('me')) {
$tids[] = $arow['id'];
}
$tid = join(',', $tids);
if($tid!='')
{
$dsql->execute("me", "select aid from jbxue_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 "; 调用整站相关文章
$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 jbxue_archives arc left join jbxue_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.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
from jbxue_archives arc left join jbxue_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.typedir,tp.typename,tp.corank,tp.isdefault,tp.defaultname,tp.namerule,
tp.namerule2,tp.ispart,tp.moresite,tp.siteurl,tp.sitepath
from jbxue_archives arc left join jbxue_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'>rn";
}
$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>rn";
for($j=0; $j < $col; $j++)
{
if($col>1) $artlist .= " <td width='$colwidth'>rn";
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['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(!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='http://item.feedsky.com/~feedsky/simplc/~8543723/686077466/5696268/1/%22%3c/code%3e%3ccode%3e.%3c/code%3e%3ccode%3e$row%3c/code%3e%3ccode%3e['typeurl']."'>".$row['typename']."</a>";
$row['image'] = "<img src='http://item.feedsky.com/~feedsky/simplc/~8543723/686077466/5696268/1/%22%3c/code%3e%3ccode%3e.%3c/code%3e%3ccode%3e$row%3c/code%3e%3ccode%3e['picname']."' border='0' width='$imgwidth' height='$imgheight' alt='".ereg_replace("['><]","",$row['title'])."'>";
$row['imglink'] = "<a href='http://item.feedsky.com/~feedsky/simplc/~8543723/686077466/5696268/1/%22%3c/code%3e%3ccode%3e.%3c/code%3e%3ccode%3e$row%3c/code%3e%3ccode%3e['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='http://item.feedsky.com/~feedsky/simplc/~8543723/686077466/5696268/1/%22%3c/code%3e%3ccode%3e.%3c/code%3e%3ccode%3e$row%3c/code%3e%3ccode%3e['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()."rn";
}
//if hasrow
else
{
$artlist .= '';
}
if($col>1) $artlist .= " </td>rn";
}
//loop col
if($col>1) $i += $col - 1;
if($col>1) $artlist .= " </tr>rn";
}
//loop line
if($col>1) $artlist .= " </table>rn";
$dsql->freeresult("al");
return $artlist;
}
?>

代码说明:
使用时记得用代码编辑器把数据库前缀批量替换成自己的,一般默认是dede_,本文中是jbxue_前缀。