>极客内参 > 技术 > 正文

Phpcms V9 调用全站文章排行的解决方法_phpcms_CMS教

今天忙于修改网站界面,想在首页添加浏览排行功能,却发现Phpcms竟然不支持调用全站文章排行。下午仔细研究了Phpcms源码,终于找到解决办法默认情况下,Phpcms只支持调用当前文章排行,代码如下:
{pc:content action="hits" catid="$catid" num="10" order="views DESC" cache="3600"}
其中$catid为待调用栏目的id,如果想实现全站调用,需要修改phpcms\modules\content\classes\content_tag.class.php文件,找到以下函数:

/**
* 排行榜标签
* @param $data
*/
public function hits($data) {
$catid = intval($data['catid']);
if(!$this->set_modelid($catid)) return false;
$this->hits_db = pc_base::load_model('hits_model');
$sql = $desc = $ids = '';
$array = $ids_array = array();
$order = $data['order'];
$hitsid = 'c-'.$this->modelid.'-%';
$sql = "hitsid LIKE '$hitsid'";
if(isset($data['day'])) {
$updatetime = SYS_TIME-intval($data['day'])*86400;
$sql .= " AND updatetime>'$updatetime'";
}
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql .= " AND catid IN ($catids_str)";
} else {
$sql .= " AND catid='$catid'";
}
$hits = array();
$result = $this->hits_db->select($sql, '*', $data['limit'], $order);
foreach ($result as $r) {
$pos = strpos($r['hitsid'],'-',2) + 1;
$ids_array[] = $id = substr($r['hitsid'],$pos);
$hits[$id] = $r;
}
$ids = implode(',', $ids_array);
if($ids) {
$sql = "status=99 AND id IN ($ids)";
} else {
$sql = '';
}
$this->db->table_name = $this->tablename;
$result = $this->db->select($sql, '*', $data['limit'],'','','id');
foreach ($ids_array as $id) {
if($result[$id]['title']!='') {
$array[$id] = $result[$id];
$array[$id] = array_merge($array[$id], $hits[$id]);
}
}
return $array;
}

修改代码(见注释):

/**
* 排行榜标签
* @param $data
*/
public function hits($data) {
$catid = intval($data['catid']);
$this->hits_db = pc_base::load_model('hits_model');
$sql = $desc = $ids = '';
$array = $ids_array = array();
$order = $data['order'];
$hitsid = 'c-'.$this->modelid.'-%';
$sql = "hitsid LIKE '$hitsid'";
if(isset($data['day'])) {
$updatetime = SYS_TIME-intval($data['day'])*86400;
$sql .= " AND updatetime>'$updatetime'";
}
if(!empty($catid) && $catid>0) { //添加判断:id是否为空
if(!$this->set_modelid($catid)) return false;
if($this->category[$catid]['child']) {
$catids_str = $this->category[$catid]['arrchildid'];
$pos = strpos($catids_str,',')+1;
$catids_str = substr($catids_str, $pos);
$sql .= " AND catid IN ($catids_str)";
} else {
$sql .= " AND catid='$catid'";
}
}
$hits = array();
$result = $this->hits_db->select($sql, '*', $data['limit'], $order);
foreach ($result as $r) {
$pos = strpos($r['hitsid'],'-',2) + 1;
$ids_array[] = $id = substr($r['hitsid'],$pos);
$hits[$id] = $r;
}
$ids = implode(',', $ids_array);
if($ids) {
$sql = "status=99 AND id IN ($ids)";
} else {
$sql = '';
}
$this->db->table_name = $this->tablename;
$result = $this->db->select($sql, '*', $data['limit'],'','','id');
foreach ($ids_array as $id) {
if($result[$id]['title']!='') {
$array[$id] = $result[$id];
$array[$id] = array_merge($array[$id], $hits[$id]);
}
}
return $array;
}

修改代码后,无论设置栏目id为0或空,都能调取全站文章排行。
调用方法1:
{pc:content action="hits" catid="0" num="10" order="views DESC" cache="3600"}
调用方法2:
{pc:content action="hits" num="10" order="views DESC" cache="3600"}


企业会员

免责声明:本网站内容主要来自原创、合作伙伴供稿和第三方自媒体作者投稿,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证有关资料的准确性及可靠性,读者在使用前请进一步核实,并对任何自主决定的行为负责。本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。任何单位或个人认为本网站中的网页或链接内容可能涉嫌侵犯其知识产权或存在不实内容时,应及时向本网站提出书面权利通知或不实情况说明,并提供身份证明、权属证明及详细侵权或不实情况证明。本网站在收到上述法律文件后,将会依法尽快联系相关文章源头核实,沟通删除相关内容或断开相关链接。

Baidu
map