## 通过栏目查找文章 /** * [getBottomArticleCategory 通过栏目查找文章] * @param [string] $cid [栏目ID] * @return [string] [返回文章] */ function getBottomArticleCategory($cid){ //1,通过对象获取文章 $field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id'; $join = [ ['__PORTAL_CATEGORY_POST__ b', 'a.id = b.post_id'] ]; $portalPostModel = new PortalPostModel(); $articles = $portalPostModel->alias('a')->field($field)->join($join) ->where(function($query) use ($cid) { $category = $cid; if (!empty($category)) { $query->where('b.category_id','in',$category); } }) ->order('update_time', 'DESC') ->select(); //2,转成数组处理 $arr=json_decode(json_encode($articles),true); foreach ($arr as $key => $val) { $url = cmf_url('portal/Article/index', ['id' => $val['id']], true, true); $nr ="<li><a href=".$url." target='_blank'>" .$val['post_title']."</a></li>"; echo $nr; } } 这里是使用对象来查询数据,具体也是拼接sql的方法获取,看不懂的话,执行下面SQL就明白了: SELECT a.*,b.id AS post_category_id,b.list_order,b.category_id FROM wys_portal_post AS a INNER JOIN wys_portal_category_post AS b ON a.id = b.post_id WHERE b.category_id IN (41) ORDER BY update_time DESC 通过中间表查询当前文章,category_id(栏目ID)可以有多个,后面是转成数组再遍历输出。 在模板里面调用 <ul class="clearfix"> <php>getBottomArticleCategory(41);</php> </ul>