您现在的位置是:首页 > cms教程 > phpcms教程phpcms教程

phpcms关联文章排序不变的解决方法

陈茗卓2025-05-04 21:38:01phpcms教程已有4人查阅

导读phpcms关联文章排序不变问题的修改方法:打开 phpcms/modules/content/classes/content_tag.class.php 内容模型标签类,发现该标签仅在内容存在人为设置的相关阅读时

phpcms关联文章排序不变问题的修改方法:
打开 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<p>其实只是将 $r = $this->db->select($sql2, '*', $limit, '','','id'); 替换为了 $r = $this->db->select($sql2, '*', $limit, $order,'','id'); 让order参数传入查询方法。<br>在模板当中,使用如下标签,加上order参数即可实现排序了。<br></p><p><span>立即学习</span>“<a href="https://pan.quark.cn/s/7fc7563c4182" style="text-decoration: underline !important; color: blue; font-weight: bolder;" rel="nofollow" target="_blank">PHP免费学习笔记(深入)</a>”;</p><p>代码如下:</p><pre class="brush:php;toolbar:false">{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']) &amp;&amp; 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
在模板中,使用如下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在未来应该会进行修正的,以上方法提供给那些心急的站长朋友们。

本文标签:

很赞哦! (0)

暂无内容
暂无内容
暂无内容
暂无内容
留言与评论 (共有 0 条评论)
昵称:
匿名发表 登录账号
         
验证码: