phpcms v9關(guān)聯(lián)文章排序陳舊問題的修改方法
之前一直沒有注意過相關(guān)閱讀的排序問題,今天偶爾看帖有網(wǎng)友說道,才發(fā)現(xiàn),果真如此。調(diào)用出來的內(nèi)容十分陳舊。于是嘗試添加 order="id DESC" 參數(shù)進(jìn)行排序,調(diào)用順序依然毫無變化。打開 phpcms/modules/content/classes/content_tag.class.php 內(nèi)容模型標(biāo)簽類一看,發(fā)現(xiàn)該標(biāo)簽僅在內(nèi)容存在人為設(shè)置的相關(guān)閱讀時(shí),才依照order參數(shù)進(jìn)行排序。而當(dāng)內(nèi)容不存在人為設(shè)置的相關(guān)閱讀時(shí),則按照關(guān)鍵字進(jìn) 行查詢,但此時(shí)并沒有按照order參數(shù)進(jìn)行排序。而是不進(jìn)行排序。這也就是為什么文章調(diào)用的相關(guān)閱讀總是那么陳舊的原因了。
修正該問題的方法如下:
修改 phpcms/modules/content/classes/content_tag.class.php 內(nèi)容模型標(biāo)簽類文件,將 content_tag 類中 relation 方法修改為:
/**
* 相關(guān)文章標(biāo)簽
* @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;
}
其實(shí)只是將 $r = $this->db->select($sql2, '*', $limit, '','','id'); 替換為了 $r = $this->db->select($sql2, '*', $limit, $order,'','id'); 讓order參數(shù)傳入查詢方法。
在模板當(dāng)中,使用如下標(biāo)簽,加上order參數(shù)即可實(shí)現(xiàn)排序了。
{pc:content action="relation" relation="$relation" id="$id" catid="$catid" num="5" keywords="$rs[keywords]" order="id DESC"}
{loop $data $r}
{/loop}
{/pc}
如果有潔癖的朋友,擔(dān)心直接修改PC會影響未來升級,可以將其單獨(dú)提取出來。放到模板中當(dāng)作函數(shù)使用。代碼如下:
<?php
/**
* 內(nèi)容模型 - 相關(guān)文章標(biāo)簽(修正排序異常問題)
* @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}
其實(shí)只是一個小問題,PC在未來應(yīng)該會進(jìn)行修正的,以上方法提供給那些心急的站長朋友們。
上一篇:phpcms內(nèi)容詳情頁只顯示日期不顯示時(shí)間的方法
欄 目:phpcms
本文標(biāo)題:phpcms v9關(guān)聯(lián)文章排序陳舊問題的修改方法
本文地址:http://mengdiqiu.com.cn/a1/phpcms/9795.html
您可能感興趣的文章
- 01-10phpcms常見問題解答
- 01-10phpcms語法規(guī)則
- 01-10PHPCMS網(wǎng)站轉(zhuǎn)移空間教程
- 01-10PHPCMS2008 SP4 心情排行指數(shù)不顯示的解決辦法
- 01-10phpcms頻道首頁調(diào)用所有一級欄目及二級欄目
- 01-10phpcms標(biāo)簽?zāi)0寮皩n}模板的制作
- 01-10PHPCMS系統(tǒng)自帶標(biāo)簽說明
- 01-10為PHPCMS 2008 編輯器增加常用中文字體方法
- 01-10PHPcms 2008 注冊選擇模型關(guān)閉后,注冊不能自動登錄的問題
- 01-10phpcms模仿QQ和MSN消息提示的效果


閱讀排行
本欄相關(guān)
- 01-10phpcms常見問題解答
- 01-10phpcms語法規(guī)則
- 01-10PHPCMS2008 SP4 心情排行指數(shù)不顯示的解
- 01-10PHPCMS網(wǎng)站轉(zhuǎn)移空間教程
- 01-10PHPCMS系統(tǒng)自帶標(biāo)簽說明
- 01-10phpcms標(biāo)簽?zāi)0寮皩n}模板的制作
- 01-10phpcms頻道首頁調(diào)用所有一級欄目及二
- 01-10PHPcms 2008 注冊選擇模型關(guān)閉后,注冊
- 01-10為PHPCMS 2008 編輯器增加常用中文字體
- 01-10phpcms模仿QQ和MSN消息提示的效果