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

时间:2022-10-19 03:52:59

打开 index.php 添加 fun函数一个,需放在<php与?>中间。

/**
* 获得指定栏目的文章列表。
* @param int $cid 栏目ID
* @param int $row 条数
* @return array
*/
function index_get_articles($cid = 0, $row = 10){
$ta = $GLOBALS['ecs']->table('article');
$tac = $GLOBALS['ecs']->table('article_cat');
$sql = "
SELECT a.article_id, a.title, ac.cat_name, a.add_time, a.file_url, a.open_type, ac.cat_id, ac.cat_name
FROM $ta AS a, $tac AS ac
WHERE a.cat_id = ac.cat_id AND a.cat_id = $cid
ORDER BY a.article_type DESC, a.add_time DESC LIMIT $row
";
$res = $GLOBALS['db']->getAll($sql); $arr = array();
foreach ($res AS $idx => $row){
$arr[$idx]['id'] = $row['article_id'];
$arr[$idx]['title'] = $row['title'];
$arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?
sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];
$arr[$idx]['cat_name'] = $row['cat_name'];
$arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);
$arr[$idx]['url'] = $row['open_type'] != 1 ?
build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']);
$arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);
$arr[$idx]['cat_id'] = $row['cat_id'];
}
return $arr;
}

第二步,查找以下代码,并添加代码所示内容:

$smarty->assign('invoice_list',    index_get_invoice_query());  // 发货查询
$smarty->assign('new_articles', index_get_new_articles()); // 最新文章
$smarty->assign('group_buy_goods', index_get_group_buy()); // 团购商品
$smarty->assign('auction_list', index_get_auction()); // 拍卖活动
$smarty->assign('shop_notice', $_CFG['shop_notice']); // 商店公告
// 找到上边的代码,添加下边这行(8是栏目ID,9是条数):
$smarty->assign('service_article', index_get_articles(8,9)); //售后服务文章

第三步,在你需要调用的dwt文件或者lib文件里加入以下代码:

<ul class="arcList">
<!--{foreach from=$service_article name=service_article item=article}-->
<li><a href="{$article.url}" title="{$article.title|escape:html}">{$article.title}</a></li>
<!--{/foreach}-->
</ul>