做项目的时候,在用ThinkCMF在首页调用指定的栏目文章,但是没有找到好的方法,就自己写了一个。

但是又不想写标签,就在公用方法里面实现了:找到common.php

操作数据库,要用到think的控制器和think的数据库类:

  1. <?php
  2. use think\Controller;
  3. use think\Db;
  4. /*
  5. * $cid 是栏目下的id
  6. */
  7. function articlelist($cid,$limit=5){
  8. $id = intval($cid);
  9. // 获取当前栏目下的所有子栏目ID
  10. $category_list = Db::name('portal_category')->field(array('id','parent_id'))->select();
  11. $category_arr = array();
  12. $cur_category_list = array();
  13. array_push($cur_category_list,intval($id));
  14. foreach($category_list as $v){
  15. $arr = array();
  16. $arr['id'] = $v['id'];
  17. $arr['pid'] = $v['parent_id'];
  18. array_push($category_arr,$arr);
  19. }
  20. //得到当前栏目所有的子栏目ids
  21. $cur_category_ids = array_merge($cur_category_list,get_all_child($category_arr,$id));
  22. //获取当前所有子栏目的所有文章ids
  23. $all_post_ids_arr = array();
  24. $all_post_ids = Db::name('portal_category_post')->where('category_id','in',$cur_category_ids)->field(array('post_id'))->select();
  25. foreach($all_post_ids as $v){
  26. array_push($all_post_ids_arr,$v['post_id']);
  27. }
  28. // 获取当前栏目及子栏目所有的文章列表
  29. $where = array();
  30. $where['id'] = array('in',$all_post_ids_arr);
  31. $where['post_type'] = 1;
  32. $where['post_status'] = 1;
  33. $pagelist_arr = Db::name('portal_post')->where($where)->paginate($limit);
  34. $page = $pagelist_arr->render();
  35. // 处理跳转链接 和 栏目的图片
  36. $pagelist = array();
  37. foreach($pagelist_arr as $k => $v){
  38. $v['thumb'] = '/upload/'.json_decode($v['more'],true)['thumbnail'];
  39. $v['category_id'] = get_category_id($v['id']);
  40. array_push($pagelist,$v);
  41. }
  42. return $pagelist;
  43. }
  44. function get_category_id($id){
  45. $categoryinfo = Db::name('portal_category_post')->where('post_id','eq',$id)->field('category_id')->find();
  46. return $categoryinfo['category_id'];
  47. }

怎么调用?

  1. <?php $articlelist = articlelist(4,7);?>
  2. <foreach name="articlelist" item="v">
  3. <dl class="partTwoChangeBoxCom o">
  4. <dt class="ab"><a href="{:cmf_url('portal/Article/index',array('id'=>$v['id'],'cid'=>$v['category_id']))}"><img src="__ROOT__{$v['thumb']}" width="324" height="224" alt=""></a></dt>
  5. <dd class="text ab">
  6. <a href="{:cmf_url('portal/Article/index',array('id'=>$v['id'],'cid'=>$v['category_id']))}" class="pr">
  7. <span class="line palt db"></span>
  8. <span class="text db">{$v['post_title']}</span>
  9. <span class="eng db">{$v['url']}</span>
  10. <span class="icon db part"></span>
  11. </a>
  12. </dd>
  13. </dl>
  14. </foreach>

即可

ThinkCMF----调用指定栏目的文章列表的更多相关文章

  1. dedecms调用子栏目及文章列表

    使用DEDECMS程序建网站时,有些栏目下面有子栏目,我们需要在网站前台调用出子栏目以及子栏目下的文章列表. dedecms调用子栏目及文章列表可以使用以下的代码进行调用: <div class ...

  2. ecshop调用指定栏目下的文章的方法

    打开 index.php 添加 fun函数一个,需放在<php与?>中间. /** * 获得指定栏目的文章列表. * @param int $cid 栏目ID * @param int $ ...

  3. 转载]PhpCms V9调用指定栏目子栏目文章的两种方法

    PhpCms V9调用指定栏目子栏目文章的两种方法 第一种.直接写子栏目id ,用cat in {pc:get sql="SELECT * from v9_news where status ...

  4. dede 怎样调用其它栏目的文章或者缩略图列表且有分页效果?

    提问i:我做一个站点.有5个栏目,第5个栏目想把前4个栏目的文章都调用一下,搞一个汇总的文章集合. 发现用arclist调用不能设置pagesize的属性.从而不能给文章分页.然而list貌似不能调用 ...

  5. PhpCms V9调用指定栏目子栏目文章的方法

    PhpCms V9调用指定栏目子栏目文章的方法 第一种,直接写父类id {pc:content action="lists" catid="父类id" num= ...

  6. 帝国cms如何调用指定id的文章到首页?

    小美女用帝国cms建了一个站,现在首页调出指定id为12的文章作为推荐,附带文章摘要,她想用代码的形式直接掉用,以后方便修改.那么,帝国cms如何调用指定id的文章呢?指定信息id,不是调用指定栏目i ...

  7. dedecmsV5.7 arclist 如何调用副栏目的文章

    问题:用arclist 调用某个栏目下的文章的时候,发现无法调用出副栏目是这个栏目的文章. 然后就上百度搜了一番,记录一下我搜到的解决方法: 1.打开/include/taglib/arclist.l ...

  8. wordpress调用指定tag的文章

    前面的文章wordpress调用指定分类文章如何实现有网友回复要如何调用指定tag的文章,原理是类似的,有两种方法,随ytkah一起来看看 1.第一种 <?php $args=array( 't ...

  9. dede织梦跨频道调用指定栏目文章的解决方法

    很久没有写技术类的文章了,这次这个标题写的- 呃, 有一点儿纠结. 事情是这样的,刚刚回答了一个百度问答上的问题,这个问题的大体意思是,有一个图片栏目,内含3个子栏目,分别为图片栏目1.2和3,另有三 ...

随机推荐

  1. getIsDebuggable

    /* * if set  android:debuggable="true" in  Manifest, return true. * if set android:debugga ...

  2. C++中使用ODBC API访问数据库例程

    使用ODBC API访问数据库简单流程,供参考使用:  ODBC API 123456789101112131415161718192021222324252627282930313233343536 ...

  3. 系统windows进程的资源分配

    http://www.captaincodeman.com/2011/02/27/limit-mongodb-memory-use-windows/ CaptainCodeman About Arch ...

  4. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  5. 8 -- 深入使用Spring -- 3...4 在ApplicationContext中使用资源

    8.3.4 在ApplicationContext中使用资源 不管以怎样的方式创建ApplicationContext实例,都需要为ApplicationContext指定配置文件,Spring允许使 ...

  6. iOS 优秀文章网址收录

    1. iOS应用支持IPV6,就那点事儿 地址:http://www.jianshu.com/p/a6bab07c4062 2. iOS配置IPV6网络 地址:http://www.jianshu.c ...

  7. Create maintenance backup plan in SQL Server 2008 R2 using the wizard

    You will need to identify how you want your maintenance plan to be setup. In this example the mainte ...

  8. Ansible 实战:一键安装 LNMP

    Ansible 配置文件 : [root@center /data/ansiblework]# cat ansible.cfg [defaults] remote_user = root remote ...

  9. ASP.NET MVC4优化

    删除无用的视图引擎 默认情况下,ASP.NET MVCE同时支持WebForm和Razor引擎,而我们通常在同一个项目中只用到了一种视图引擎,如Razor,那么,我们就可以移除掉没有使用的视图引擎,提 ...

  10. Expected BEGIN_ARRAY but was BEGIN_OBJECT

    Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 3519 path $.data[1].inspector_user Gson 中 ...