帝国cms怎么调用文章的评论数量和列表调用方法
调用文章评论数量
要在列表页或内容页显示某篇文章的评论总数,可以通过以下几种方式实现:
方法一:使用SQL函数调用(适用于列表页)
在列表模板(list.var)中使用PHP代码查询评论数:
开启“使用程序代码”选项(在模板变量中勾选“使用程序代码”)
使用如下代码:
$select = $empire->query("SELECT COUNT(*) as total FROM {$dbtbpre}enewspl_1 WHERE id='$r[id]' AND classid='$r[classid]'");
$plnum = $empire->fetch($select);
$plcount = $plnum['total'] ? $plnum['total'] : 0;
$listtemp = '<li><a href="[!--titleurl--]">[!--title--]</a> (评论:{$plcount})</li>';
说明:此方法通过查询评论表 enewspl_1(根据模型不同可能有变化)统计对应ID和栏目ID的文章评论数。
方法二:使用灵动标签调用(推荐用于内容页)
在内容页模板中,可直接使用灵动标签结合PHP函数:
<?php
$comment_num = $empire->gettotal("SELECT COUNT(*) AS total FROM {$dbtbpre}enewspl_1 WHERE id='$navinfor[id]' AND classid='$class_r[$GLOBALS[navclassid]][classid]'");
?>
评论数量:<?= $comment_num ?>
注意:需确保开启模板支持PHP代码功能。
调用文章评论列表
如果想在内容页下方展示该文章的所有评论内容,可以使用灵动标签循环输出评论数据。
[e:loop={"select saytext,uname,ip,zftime from {$dbtbpre}enewspl_1 where classid='$navinfor[classid]' and id='$navinfor[id]' order by zftime desc limit 10",10,24,0}]
<div class="comment-item">
<strong>用户名:<?= $bqr[uname] ?></strong>
<span>(<?=date('Y-m-d H:i',$bqr[zftime])?>)</span>
<p>评论内容:<?=htmlspecialchars($bqr[saytext])?></p>
</div>
[/e:loop]
说明:
查询字段包括评论内容(saytext)、用户名(uname)、IP(ip)、时间(zftime)
条件限制为当前文章id和classid
按时间倒序排列,最多显示10条






