phpcms v9默认后台有类别管理,但是没有按照类别浏览的功能,本文正是为了弥补这个不足。
在需要调用类别的地方,比如列表页,首先写循环前面加一句把类别缓存加载进来:
<?php $TYPE = getcache('type_content','commons');?>
然后在循环里写:
<a href="index.php?m=content&c=type&catid={$catid}&typeid={$r[typeid]}">{$TYPE[$r[typeid]][name]}</a>
这样就可以调用出来类别了,不过链接暂时无效,因为暂时还没有type这个控制器及模板,接下来会新增两个文件。
1、类别页面模板:
新增一个文件 phpcms\templates\default\content\type.html ,代码如下:
{template "content","header"}
<!--main-->
<?php $typeid = intval($_GET['typeid']);?>
<div class="main">
<div class="col-left">
<div class="crumbs">
<a href="{siteurl($siteid)}">首页</a><span> > </span>{catpos($catid)}
类别:<font color="red">{$TYPE[$typeid][name]}</font> 总共有 {$total} 条记录
</div>
<ul class="list lh24 f14">
{loop $datas $r}
<li><span class="rt">{date('Y-m-d H:i:s',$r[inputtime])}</span>
·<a href="{$r[url]}" target="_blank"{title_style($r[style])}>{$r[title]}</a></li>
{if $n%5==0}
<li class="bk20 hr"></li>
{/if}
{/loop}
</ul>
<div id="pages" class="text-c">{$pages}</div>
</div>
<div class="col-auto">
<div class="box">
<h5 class="title-2">频道总排行</h5>
{pc:content action="hits" catid="$catid" num="10" order="views DESC"}
<ul class="content digg">
{loop $data $r}
<li><a href="{$r[url]}" target="_blank">{$r[title]}</a></li> {/loop}
</ul>
{/pc}
</div>
</div>
</div>
{template "content","footer"}
2、类别控制器
新增一个文件 phpcms\modules\content\type.php ,代码如下:
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
//模型缓存路径
define('CACHE_MODEL_PATH',CACHE_PATH.'caches_model'.DIRECTORY_SEPARATOR.'caches_data'.DIRECTORY_SEPARATOR);
pc_base::load_app_func('util','content');
class type {
private $db;
function __construct() {
$this->db = pc_base::load_model('content_model');
}
/**
* 按照模型搜索
*/
public function init() {
if(!isset($_GET['catid'])) showmessage(L('missing_part_parameters'));
$catid = intval($_GET['catid']);
$siteids = getcache('category_content','commons');
$siteid = $siteids[$catid];
$this->categorys = getcache('category_content_'.$siteid,'commons');
if(!isset($this->categorys[$catid])) showmessage(L('missing_part_parameters'));
if(isset($_GET['info']['catid']) && $_GET['info']['catid']) {
$catid = intval($_GET['info']['catid']);
} else {
$_GET['info']['catid'] = 0;
}
if(isset($_GET['typeid']) && trim($_GET['typeid']) != '') {
$typeid = intval($_GET['typeid']);
} else {
showmessage(L('illegal_operation'));
}
$TYPE = getcache('type_content','commons');
$modelid = $this->categorys[$catid]['modelid'];
$modelid = intval($modelid);
if(!$modelid) showmessage(L('illegal_parameters'));
$CATEGORYS = $this->categorys;
$siteid = $this->categorys[$catid]['siteid'];
$siteurl = siteurl($siteid);
$this->db->set_model($modelid);
$page = $_GET['page'];
$datas = $infos = array();
$infos = $this->db->listinfo("`typeid` = '$typeid'",'id DESC',$page,20);
$total = $this->db->number;
if($total>0) {
$pages = $this->db->pages;
foreach($infos as $_v) {
if(strpos($_v['url'],'://')===false) $_v['url'] = $siteurl.$_v['url'];
$datas[] = $_v;
}
}
$SEO = seo($siteid, $catid, $TYPE[$typeid]['name'],$TYPE[$typeid]['description'],$TYPE[$typeid]['name'].'类别');
include template('content','type');
}
}
?>
上面的链接即可生效。