如何解決phpcms關(guān)聯(lián)文章排序不變的問題

這篇文章運(yùn)用簡單易懂的例子給大家介紹如何解決phpcms關(guān)聯(lián)文章排序不變的問題,代碼非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、青岡網(wǎng)絡(luò)推廣、小程序設(shè)計、青岡網(wǎng)絡(luò)營銷、青岡企業(yè)策劃、青岡品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供青岡建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn

phpcms關(guān)聯(lián)文章排序不變問題的修改方法:

打開 phpcms/modules/content/classes/content_tag.class.php 內(nèi)容模型標(biāo)簽類,發(fā)現(xiàn)該標(biāo)簽僅在內(nèi)容存在人為設(shè)置的相關(guān)閱讀時,則依照order參數(shù)進(jìn)行排序。而當(dāng)內(nèi)容不存在人為設(shè)置的相關(guān)閱讀時,則按照關(guān)鍵字進(jìn)行查詢,但此時并沒有按照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}

關(guān)于如何解決phpcms關(guān)聯(lián)文章排序不變的問題就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

網(wǎng)頁名稱:如何解決phpcms關(guān)聯(lián)文章排序不變的問題
文章來源:http://muchs.cn/article48/pipeep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、虛擬主機(jī)網(wǎng)站營銷、、動態(tài)網(wǎng)站網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)