ecshop调用指定栏目下的文章的方法
打开 index.php 添加 fun函数一个,需放在<php与?>中间。
1 /** 2 * 获得指定栏目的文章列表。 3 * @param int $cid 栏目ID 4 * @param int $row 条数 5 * @return array 6 */ 7 function index_get_articles($cid = 0, $row = 10){ 8 $ta = $GLOBALS['ecs']->table('article'); 9 $tac = $GLOBALS['ecs']->table('article_cat'); 10 $sql = " 11 SELECT a.article_id, a.title, ac.cat_name, a.add_time, a.file_url, a.open_type, ac.cat_id, ac.cat_name 12 FROM $ta AS a, $tac AS ac 13 WHERE a.cat_id = ac.cat_id AND a.cat_id = $cid 14 ORDER BY a.article_type DESC, a.add_time DESC LIMIT $row 15 "; 16 $res = $GLOBALS['db']->getAll($sql); 17 18 $arr = array(); 19 foreach ($res AS $idx => $row){ 20 $arr[$idx]['id'] = $row['article_id']; 21 $arr[$idx]['title'] = $row['title']; 22 $arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ? 23 sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title']; 24 $arr[$idx]['cat_name'] = $row['cat_name']; 25 $arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']); 26 $arr[$idx]['url'] = $row['open_type'] != 1 ? 27 build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']); 28 $arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']); 29 $arr[$idx]['cat_id'] = $row['cat_id']; 30 } 31 return $arr; 32 }
第二步,查找以下代码,并添加代码所示内容:
1 $smarty->assign('invoice_list', index_get_invoice_query()); // 发货查询 2 $smarty->assign('new_articles', index_get_new_articles()); // 最新文章 3 $smarty->assign('group_buy_goods', index_get_group_buy()); // 团购商品 4 $smarty->assign('auction_list', index_get_auction()); // 拍卖活动 5 $smarty->assign('shop_notice', $_CFG['shop_notice']); // 商店公告 6 // 找到上边的代码,添加下边这行(8是栏目ID,9是条数): 7 $smarty->assign('service_article', index_get_articles(8,9)); //售后服务文章
第三步,在你需要调用的dwt文件或者lib文件里加入以下代码:
1 <ul class="arcList"> 2 <!--{foreach from=$service_article name=service_article item=article}--> 3 <li><a href="{$article.url}" title="{$article.title|escape:html}">{$article.title}</a></li> 4 <!--{/foreach}--> 5 </ul>