ECSHOP新页面调用指定文章分类下的最新文章列表显示文章标题文章缩略图图片文章简介
为了个性化需求,需要在首页或其它模板页面调用指定分类下的文章,那么我们可以这样来实现:
网上很容易找到这个教程,但都有一个问题,调用的文章不是按最新排序的,就是发布时间越早的排前面,肯定是不合理的,一流资源网已修修这个问题 。
1、先打开index.php文件找到以下代码:
$smarty
->assign(
'new_articles'
, index_get_new_articles());
// 最新文章
在这行下面增加以下代码:
$smarty
->assign(
'class_articles_4'
, index_get_class_articles(4,6));
// 分类调用文章
4为文章分类ID,6就是显示条数。
若调用很多分类的话,只需要将上面一行复制粘贴即可,但文章分类id和调用数量要根据自己需要进行修改。
打开ecshop根目录下的includes/init.php文件,以底部添加以下代码,添加到 ?> 以内。
2、还在这个文件 ?> 这个之前增加以下函数:
/**
* 获得指定栏目最新的文章列表。
*
* @access private
* @return array
*/
function
index_get_class_articles(
$cat_aid
,
$cat_num
)
{
$sql
=
"SELECT article_id, title,open_type,cat_id,file_url,description FROM "
.
$GLOBALS
[
'ecs'
]->table(
'article'
).
" WHERE cat_id = "
.
$cat_aid
.
" and is_open = 1 ORDER BY add_time DESC LIMIT "
.
$cat_num
;
$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
][
'file_url'
] =
$row
[
'file_url'
];
$arr
[
$idx
][
'description'
] =
$row
[
'description'
];
$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'
]));
}
return
$arr
;
}
3、在index.dwt模板想调用的地方增加以下代码,(注:以下调上面设置里的分类ID为4的文章列表):
<!--{
foreach
from=
$class_articles_4
item=article}-->
<li><a href=
"{$article.url}"
title=
"{$article.title|escape:html}"
><!--{
$article
.short_title|truncate:15:true}--></a></li>
<!--{/
foreach
}-->
OK了,
以上代码,经过一流资源网改良后,已经可以调用文章图片及文章简介了:
文章图片:
{
$article
.file_url}
文章简介:
{
$article
.description}
文章图片是指文章的上传文件中的图片文件:
文章简介就是文章的网页描述。
有些教程里说将代码放在 init.php 文件里,而不是 index.php 文件里,同样有效。但是放在 init.php 里会导致网站的配送地区 无法选择。