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

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

陈茗卓2025-05-04phpcms教程已有人查阅

导读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在未来应该会进行修正的,以上方法提供给那些心急的站长朋友们。

本文标签:

很赞哦! ()

相关源码

  • (PC+WAP)货运物流快递仓储货架pbootcms网站模板下载本模板基于PbootCMS开发,为货运物流、快递配送、仓储货架等行业设计,支持PC+WAP自适应,确保在电脑、手机、平板等设备上均能流畅访问。适用于物流公司、仓储企业查看源码
  • (自适应响应式)动力刀座五金机械设备pbootcms网站源码下载为动力刀座及五金机械企业定制的响应式网站框架,基于PbootCMS系统深度开发,助力传统制造企业实现数字化转型升级。采用手工编写的DIV+CSS前端架构,代码执行效率提升40%。查看源码
  • 帝国CMS游戏应用APP推广下载站模板免费下载本模板为移动应用推广、手机游戏推广行业设计,集成H5游戏平台与APP下载功能,支持PC端与移动端自适应访问。专注于为应用开发商、游戏发行商提供专业的线上推广展示平台。查看源码
  • (自适应手机端)锁锁芯锁具网站pbootcms模板 智能防盗锁网站源码下载本模板基于PbootCMS系统开发,为智能锁具、防盗锁芯及相关安防产品企业设计。采用响应式布局技术,确保在手机、平板和电脑等不同设备上都能获得良好的浏览体验,数据实时同步更新。查看源码
  • (自适应响应式)APP应用程序软件介绍落地页源码免费下载该模板为营销技术从业者设计,提供专业的内容展示平台。采用响应式布局,适配软件介绍、APP推广等营销场景,通过可视化后台可快速搭建符合行业特性的展示网站。查看源码
  • 宽屏自适应搬家家政快递物流公司网站模板该宽屏大气的响应式网站模板专为搬家公司、家政服务及物流快递企业设计,基于PbootCMS内核开发,通过自适应布局确保手机、PC等多终端体验一致,助力企业高效构建专业在线服务平台。查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐