织梦调用相关文章时likearticle无法排除当前文章的解决方法
当我们发表一篇文章时,一般情况下为了增加用户的停留时间,都会在文章的末尾或者恰当的位置调用跟本文相关的文章,以赢得用户的再次点击,在织梦dedecms(V5.7版)调用相似文章中,实用的调用代码为{dede:likearticle},这个代码的原理是通过识别文章的标题、类目、关键词等来判断相似度,从而进行推荐,后台调用代码如下/include/taglib/likearticle.lib.php,打开文件找到下面这段代码:
if($keyword != '')
{
if(!empty($typeid)) {
$typeid ="AND arc.typeid IN($typeid) AND arc.id<>'$aid' ";
}
$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.arcrank>-1 AND ($keyword) $typeid $orderquery limit 0, $row";
}
else
{
if(!empty($typeid)) {
$typeid = " arc.typeid IN($typeid) AND arc.id<>'$aid' ";
}
$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.arcrank>-1 AND $typeid $orderquery limit 0, $row";
}
从上面的代码可以看到,织梦dedecms为了排除本文在程序一开头就试图过滤本文(AND arc.id<>'$aid' "),但是没成功,那怎么才能成功排除当前文章呢?很简单,我们在这段代码:AND ($keyword) $typeid 重新排除一次就可以,两段代码都要加,改完之后如下(注意看绿色加粗代码的地方有什么不同):
if($keyword != '')
{
if(!empty($typeid)) {
$typeid ="AND arc.typeid IN($typeid) AND arc.id<>'$aid' ";
}
$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.arcrank>-1 AND ($keyword) $typeid AND arc.id<>'$aid' $orderquery limit 0, $row";
}
else
{
if(!empty($typeid)) {
$typeid = " arc.typeid IN($typeid) AND arc.id<>'$aid' ";
}
$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.arcrank>-1 AND $typeid AND arc.id<>'$aid' $orderquery limit 0, $row";
}
到此,大功告成,赶紧去试试吧。