在 ECShop 商品詳情頁顯示「最新評論」區塊,讓訪客快速瀏覽全站最新留言,增加互動感。
編輯 includes/lib_goods.php,新增 get_latest_comment():
/**
* 取得最新評論
* @param int $limit_num 筆數
* @return array
*/
function get_latest_comment($limit_num) {
$sql = "SELECT c.content, c.add_time, g.goods_id, g.goods_name
FROM " . $GLOBALS['ecs']->table('comment') . " AS c
LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON c.id_value = g.goods_id
WHERE c.status = 1
LIMIT " . $limit_num;
return $GLOBALS['db']->getAll($sql);
}
編輯根目錄 goods.php,在指派 goods_rank 之後加入:
$smarty->assign('comments_latest', get_latest_comment(10)); // 取得最新 10 筆評論
在 library/ 目錄下建立 comments_latest.lbi,內容自行設計 HTML 結構,例如:
<div class="latest-comments">
<h3>最新評論</h3>
<ul>
<!-- {foreach from=$comments_latest item=comment} -->
<li><strong>{$comment.goods_name}</strong>: {$comment.content} <span>{$comment.add_time}</span></li>
<!-- {/foreach} -->
</ul>
</div>
編輯模板 goods.dwt,於適當位置插入:
<!-- #BeginLibraryItem "/library/comments_latest.lbi" --><!-- #EndLibraryItem -->
清除後台快取,前台商品頁即可看到最新評論區塊。可依設計需求調整 CSS 樣式、顯示筆數或加入分頁功能。