本教程以“ECSHOP2.7.2 + 官方默认模板” 为例,其他模板大同小异,, \' Y+ w; X j5 k: z4 j5 P6 G
4 \1 `2 N1 U6 `1 Q8 V! Q$ ^
效果图如下:, V9 S9 s4 \4 w. K8 z4 l
0 `2 H$ O6 m$ P4 h
% q0 j' l9 _) o! n) j% L+ X1 H
第一步:
G* f0 D# n' M将下面代码复制,并保存为一个库文件,文件名和保存路径为: /themes/default/library/index_comments.lbi , p5 V" y# q4 ?/ P8 W
( c" A# `6 w! S) u- B% T+ ~<?php
+ w/ G, Z8 F6 bif(!function_exists("get_comments")){
) m* l6 {/ I+ A' s" H, Kfunction get_comments($num)% Q( n# b/ @* ]) A% g) ?; M! W; o
{
2 ]& k2 X$ f2 |5 n# @6 { $sql = 'SELECT * FROM '. $GLOBALS['ecs']->table('comment') .8 j5 t4 [+ d( I5 v, Y
' WHERE status = 1 AND parent_id = 0 and comment_type=0 '.
+ D- C8 F: p# w$ ~# {- r ' ORDER BY add_time DESC';% O8 }3 u$ C7 `, Z3 D+ ]' G* v
if ($num > 0)8 H D& C& s4 B8 p' U
{
6 R" M( j6 [8 V( O/ A5 ? $sql .= ' LIMIT ' . $num;4 A. k, `1 H* f# ^' x+ m9 o- M
}
1 n4 Z. x7 S1 w- a4 Y" J/ {3 w7 I //echo $sql;$ c, O1 W+ L5 S: a* B
) p+ \0 k7 l: P0 O4 v h% u
$res = $GLOBALS['db']->getAll($sql);
m4 v2 z& R# m! ^7 u $comments = array();
& F' b! f$ M, }7 d V foreach ($res AS $idx => $row)5 h5 v; Y% |& A
{3 t! N# @# X7 Q0 s, W0 |
$comments[$idx]['add_time'] = $comments[$idx]['add_time'] = local_date
: U8 o: T: f7 L4 t& @($GLOBALS['_CFG']['time_format'], $row['add_time']);
0 h: H5 J1 h9 q( o" B3 m U $comments[$idx]['user_name'] = $row['user_name'];
$ }+ ]/ P! a+ ^" t) U+ Y$ R2 u $comments[$idx]['content'] = $row['content'];3 K7 o7 }; m, w8 m8 N! W& l5 @
$comments[$idx]['id_value'] = $row['id_value'];/ ^# Q7 S) E/ R0 T6 o
}
7 B A/ t( {9 h return $comments;0 E* W6 ]0 A- w' J, G" B n
}
8 j( p0 i/ m; T}* ]/ K9 z+ s/ f# a3 L
$GLOBALS['smarty']->assign('my_comments',get_comments(10)); // 10条数据
1 w$ O3 c! @* t6 m( ?/ ]" Z1 ^" b?> <meta http-equiv="Content-Type" content="text/html; charset=utf-8">. K1 |8 m O- Z+ t3 w2 r& ~/ ]
<!-- 最新评论__Begin -->
) t9 l, v; l E4 Q' r <div class="box_2"> A0 b# N' U: l" r* v+ Q9 o. |. D
<h3><span>最新评论</span></h3>; j1 f. D3 f7 U8 U) T( w
<div class="top10List clearfix">( |6 |. u; @7 }' ?$ ]
<!--{foreach from=$my_comments item=comments}-->6 i0 Q. {/ e8 U/ S" i# q+ t
<ul class="clearfix">
( E* v% m2 r& w' K6 f <li style="padding:5px 10px;">
8 V) ^' T- y% H% T8 @ <a href="goods.php?id={$comments.id_value}" target="_blank">
( R; }& G/ O2 H1 q {$comments.content|truncate:21:""}</a><br />
) u5 A' V$ @7 h) I: j S {$comments.add_time}
4 z K# Z/ ^* V. j </li>
+ Y9 w5 O# \6 s! U2 x4 j A. x: Y. Y </ul>! S) t# D) j+ z3 g$ f2 L# C9 e2 r( X& q
<!--{/foreach}-->
$ E M$ ?% _ x) e </div>/ r. C- o2 L5 E# k- V& G8 o
</div>
: l7 F5 i; j- d8 t: o5 L |
( C, k+ c1 ^' A7 \! |第二步:
8 w2 ^0 j* X' _, f K2 B3 @$ s( B1 a+ A; e# n; T4 w
在ECSHOP首页模板文件中,调用这个库文件* D$ G# f/ k7 _& d0 y
打开 themes/default/index.dwt 文件,$ P7 l+ b2 m+ I
在</div>0 z4 o- L& w" i* a" H
<!--left end--> | 上面增加一行调用代码4 K; D3 f1 h1 \9 Q' R1 k, g
<!-- #BeginLibraryItem "/library/index_comments.lbi" --> <!-- #EndLibraryItem --> | 7 t- u$ M1 H$ O9 C8 `" K% l
清除一下缓存
' p0 I' a5 x2 |- m6 r9 q |