phpcms v9关联文章排序陈旧问题的修改方法

之前一直没有注意过相关阅读的排序问题,今天偶尔看帖有网友说道,才发现,果真如此。调用出来的内容十分陈旧。于是尝试添加 order="id DESC" 参数进行排序,调用顺序依然毫无变化。打开 phpcms/modules/content/classes/content_tag.class.php 内容模型标签类一看,发现该标签仅在内容存在人为设置的相关阅读时,才依照order参数进行排序。而当内容不存在人为设置的相关阅读时,则按照关键字进 行查询,但此时并没有按照order参数进行排序。而是不进行排序。这也就是为什么文章调用的相关阅读总是那么陈旧的原因了。
修正该问题的方法如下:
修改 phpcms/modules/content/classes/content_tag.class.php 内容模型标签类文件,将 content_tag 类中 relation 方法修改为:

        /**
* 相关文章标签
* @param $data
*/
public function relation($data) {
$catid = intval($data['catid']);
if(!$this->set_modelid($catid)) return false;
$order = $data['order'];
$sql = "`status`=99";
$limit = $data['id'] ? $data['limit']+1 : $data['limit'];
if($data['relation']) {
$relations = explode('|',trim($data['relation'],'|'));
$relations = array_diff($relations, array(null));
$relations = implode(',',$relations);
$sql = " `id` IN ($relations)";
$key_array = $this->db->select($sql, '*', $limit, $order,'','id');
} elseif($data['keywords']) {
$keywords = str_replace('%', '',$data['keywords']);
$keywords_arr = explode(' ',$keywords);
$key_array = array();
$number = 0;
$i =1;
foreach ($keywords_arr as $_k) {
$sql2 = $sql." AND `keywords` LIKE '%$_k%'".(isset($data['id']) && intval($data['id']) ? " AND `id` != '".abs(intval($data['id']))."'" : '');
$r = $this->db->select($sql2, '*', $limit, $order,'','id');
$number += count($r);
foreach ($r as $id=>$v) {
if($i<= $data['limit'] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data['limit']<$number) break;
}
}
if($data['id']) unset($key_array[$data['id']]);
return $key_array;
}

其实只是将 $r = $this->db->select($sql2, '*', $limit, '','','id'); 替换为了 $r = $this->db->select($sql2, '*', $limit, $order,'','id'); 让order参数传入查询方法。
在模板当中,使用如下标签,加上order参数即可实现排序了。

{pc:content action="relation" relation="$relation" id="$id" catid="$catid" num="5" keywords="$rs[keywords]" order="id DESC"}
{loop $data $r}
{/loop}
{/pc}

如果有洁癖的朋友,担心直接修改PC会影响未来升级,可以将其单独提取出来。放到模板中当作函数使用。代码如下:

<?php
/**
* 内容模型 - 相关文章标签(修正排序异常问题)
* @param $data
*/
function mk1_content_tag_relation($data) {
$db = pc_base::load_model('content_model');
$catid = intval($data['catid']);
$siteids = getcache('category_content','commons');
if(!$siteids[$catid]) return false;
$siteid = $siteids[$catid];
$category = getcache('category_content_'.$siteid,'commons');
if(empty($category)) return false;
if($category[$catid]['type']!=0) return false;
$db->set_model($category[$catid]['modelid']);
$order = $data['order'];
$sql = "`status`=99";
$limit = $data['id'] ? $data['limit']+1 : $data['limit'];
if($data['relation']) {
$relations = explode('|',trim($data['relation'],'|'));
$relations = array_diff($relations, array(null));
$relations = implode(',',$relations);
$sql = " `id` IN ($relations)";
$key_array = $db->select($sql, '*', $limit, $order,'','id');
} elseif($data['keywords']) {
$keywords = str_replace('%', '',$data['keywords']);
$keywords_arr = explode(' ',$keywords);
$key_array = array();
$number = 0;
$i =1;
foreach ($keywords_arr as $_k) {
$sql2 = $sql." AND `keywords` LIKE '%$_k%'".(isset($data['id']) && intval($data['id']) ? " AND `id` != '".abs(intval($data['id']))."'" : '');
$r = $db->select($sql2, '*', $limit, $order,'','id');
$number += count($r);
foreach ($r as $id=>$v) {
if($i<= $data['limit'] && !in_array($id, $key_array)) $key_array[$id] = $v;
$i++;
}
if($data['limit']<$number) break;
}
}
if($data['id']) unset($key_array[$data['id']]);
return $key_array;
}
?>

在模板中,使用如下PHP代码获取即可。


{php $data = mk1_content_tag_relation(array('relation'=>$relation,'id'=>$id,'catid'=>$catid,'keywords'=>$rs['keywords'],'order'=>'id DESC','limit'=>'4')); }
{loop $data $r}
{/loop}

其实只是一个小问题,PC在未来应该会进行修正的,以上方法提供给那些心急的站长朋友们。