phpcms如何在前台文章列表前显示所属类别名称

最近做单位网站模版遇到的问题,欲实现的效果:

 

但是phpcms中自带的文章列表标签没有这个功能,数据库中文章表中也只有类别id的字段,因此不能通过简单的{$r[catname]}读出来类别名。

从网上查询找到的解决方案是这样的【phpcms-v9】利用[后台]->[类别管理]为文章前后台添加类别名称,其中比较实用的方案是直接从缓存中读取类别信息,但这个方案适用的phpcms版本较老了,用了没效果,经过查看分析源码,找到解决方案如下(适用于phpcms_v9.5.9版本):

1、打开phpcms\caches\caches_commons\caches_data\category_content_1.cache.php

 1 return array (
 2   1 => 
 3   array (
 4     'catid' => '1',
 5     'siteid' => '1',
 6     'type' => '1',
 7     'modelid' => '0',
 8     'parentid' => '0',
 9     'arrparentid' => '0',
10     'child' => '1',
11     'arrchildid' => '1,2,3,5,162',
12     'catname' => '网站介绍',
13     'style' => '',
14     'image' => '',
15     'description' => '',
16     'parentdir' => '',
17     'catdir' => 'about',
18     'url' => '/html/about/',
19     'items' => '0',
20     'hits' => '0',
21     'setting' => 'array (
22   \'ishtml\' => \'1\',
23   \'template_list\' => \'default\',
24   \'page_template\' => \'page\',
25   \'meta_title\' => \'\',
26   \'meta_keywords\' => \'\',
27   \'meta_description\' => \'\',
28   \'category_ruleid\' => \'1\',
29   \'show_ruleid\' => \'\',
30   \'repeatchargedays\' => \'1\',
31 )',
category_content_1.cache.php

可以看到,这里缓存了所有栏目类别的详细信息,其中就有我们需要的

'catname' => '网站介绍',

2、打开phpcms\modules\content\classes\content_tag.class.php

 1 /**
 2      * 列表页标签
 3      * @param $data
 4      */
 5     public function lists($data) {
 6         $catid = intval($data['catid']);
 7         if(!$this->set_modelid($catid)) return false;
 8         if(isset($data['where'])) {
 9             $sql = $data['where'];
10         } else {
11             $thumb = intval($data['thumb']) ? " AND thumb != ''" : '';
12             if($this->category[$catid]['child']) {
13                 $catids_str = $this->category[$catid]['arrchildid'];
14                 $pos = strpos($catids_str,',')+1;
15                 $catids_str = substr($catids_str, $pos);
16                 $sql = "status=99 AND catid IN ($catids_str)".$thumb;
17             } else {
18                 $sql = "status=99 AND catid='$catid'".$thumb;
19             }
20         }
21         $order = $data['order'];
22 
23         $return = $this->db->select($sql, '*', $data['limit'], $order, '', 'id');
content_tag.class.php

3、在上述代码后加入以下代码

1 $TYPES = getcache('category_content_1','commons');//获取类别缓存
2         foreach ($return as $key=>$v) {                                       
3             $return[$key][typename]=$TYPES[$v['catid']]['catname'];//给lists标签返回的数组中增加一个类别字段 
4         }

4、前台调用方法

{$v[typename]}

 

posted @ 2015-07-03 12:51  captainmoore  阅读(953)  评论(0)    收藏  举报