在Application/Home(或相關 模型)/Common/function.php中新增如下函式:
/** * 獲取指定分類的子分類 * @param int $id 分類ID * @param int $level 子分類級別(1:一級子分類,2:一至二級子分類,3:一至三級子分類,n:可擴充套件至N級) * @param mixed $extend 用於巢狀迴圈時附加巢狀前資料(預設false) * @reuturn array 對應分類資料 */ function get_children_by_category($id, $level=1, $extend=false) { (!is_numeric($id) || !is_numeric($level)) && exit; $value = $extend ? $extend : array(); switch ($level) { case 1: $son_ids = D('Category')->getChildrenId($id); if (!empty($son_ids)) { if ($extend) { foreach ($extend as $key=>$val) { $extend_son_ids = D('Category')->getChildrenId($val['id']); if (!empty($extend_son_ids)) { $extend_son_ids = strpos($extend_son_ids, ',') ? explode(',', $extend_son_ids) : array($extend_son_ids); $value[$key]['_son'] = D('Category')->getSameLevel($extend_son_ids[0]); } } } else { $son_ids = strpos($son_ids, ',') ? explode(',', $son_ids) : array($son_ids); $value = D('Category')->getSameLevel($son_ids[0]); } } break; case 2: $value = get_children_by_category($id, 1); $value = get_children_by_category($id, 1, $value); break; case 3: $value = get_children_by_category($id, 2); $value = get_children_by_category($id, 1, $value); break; } return $value; }模板中直接呼叫:
<php>$son = get_children_by_category(分類ID,1);</php> <if condition="$son neq ''"> <ul> <foreach name="son" item="son_list"> <li><a href="{:get_info_by_category($son_list['id'])}">{$son_list.title}</a></li> </foreach> </ul> </if>