typecho统计父级评论下面的“子级评论”有多少条
8个月前 最近用独立页面编写的一个朋友圈模板,基于评论功能实现。 为了实现显...
第一步在需要引入jquery库,这一步不能少,可以使用本地js或者下面的cdn或者其他的cdn(一般都放在主题中的footer.php)
第二步把下面的html代码和js代码放到主题相关文件中(一般都放在主题中的footer.php)
**html表单**
class="btn-comment bg-[#07c160] border-0 outline-none text-white cursor-pointer rounded-sm">回复
**js代码**
第三步在把下面的代码添加到相关文件中,(一般index.php),找到这段代码“next()):
?>//此次省略无数代码”代码里面
第四步把下面的代码添加到相关文件中(一般function.php)
// 主题初始化
function themeInit($self)
{
$route = $self->request->getPathInfo();
/* 主题开放API 路由规则 */
if ($self->request->isPost()) {
switch ($route) {
case '/api/comment':
addComment($self);
break;
}
}
}
/**
* 获取网站首页地址。
*/
function getWebsiteHomeUrl()
{
$rewrite = Helper::options()->rewrite;
$tmpUrl = Helper::options()->siteUrl;
if (!$rewrite) {
$tmpUrl = $tmpUrl . 'index.php';
}
return $tmpUrl;
}
/**
* 用于列表文章查询相关文章评论
* 通过文章cid查询评论数据函数
* 参数一 文章cid
* 参数二 要查询多少条数据
*/
function listcomment($archive,$limits = 3){
$db = Typecho_Db::get();
return $db->fetchAll($db->select()->from('table.comments')
->where("cid = ? AND status = 'approved' AND type = 'comment'", $archive)
->limit($limits)->order('created', Typecho_Db::SORT_DESC)
);
}
//评论加@或者回复
function reply($parent,$authorId) {
if ($parent == 0) {
return '';
}
$db = Typecho_Db::get();
$commentInfo = $db->fetchRow($db->select('authorId,author,status,mail')->from('table.comments')->where('coid = ?', $parent));
$zuozhe = "";
if ($commentInfo['authorId'] == $authorId) {
$zuozhe = "作者";
}
$link = ' 回复 '.$commentInfo['author'].$zuozhe.'';
return $link;
}
/**
* 添加评论
*/
function addComment($self)
{
$self->response->setStatus(200);
$options = Helper::options();
$user = Typecho_Widget::widget('Widget_User');
$db = Typecho_Db::get();
// // Security 验证不通过时会直接跳转,所以需要自己进行判断
// // 需要开启反垃圾保护,此时将不验证来源
// if ($self->request->get('_') != Helper::security()->getToken($self->request->getReferer())) {
// $self->response->throwJson(array('status' => 0, 'msg' => _t('非法请求')));
// }
/** 检查ip评论间隔 */
if (!$user->pass('editor', true) && $self->authorId != $user->uid && $options->commentsPostIntervalEnable) {
$latestComment = $db->fetchRow($db->select('created')->from('table.comments')
->where('cid = ?', $self->request->cid)
->where('ip = ?', $self->request->getIp())
->order('created', Typecho_Db::SORT_DESC)
->limit(1));
if ($latestComment && ($options->gmtTime - $latestComment['created'] > 0 && $options->gmtTime - $latestComment['created'] < $options->commentsPostInterval)) {
$self->response->throwJson(array('status' => 0, 'msg' => _t('对不起, 您的发言过于频繁, 请稍侯再次发布')));
}
}
$comment = array(
'cid' => $self->request->cid,
'created' => $options->gmtTime,
'agent' => $self->request->getAgent(),
'ip' => $self->request->getIp(),
'ownerId' => $self->author->uid,
'type' => 'comment',
'status' => $options->commentsRequireModeration ? 'waiting' : 'approved'
);
/** 判断父节点 */
if ($parentId = $self->request->filter('int')->get('parent')) {
if ($options->commentsThreaded && ($parent = $db->fetchRow($db->select('coid', 'cid')->from('table.comments')->where('coid = ?', $parentId))) && $self->request->cid == $parent['cid']) {
$comment['parent'] = $parentId;
} else {
$self->response->throwJson(array('status' => 0, 'msg' => _t('父级评论不存在')));
}
}
$feedback = Typecho_Widget::widget('Widget_Feedback');
//检验格式
$validator = new Typecho_Validate();
$validator->addRule('author', 'required', _t('必须填写用户名'));
$validator->addRule('author', 'xssCheck', _t('请不要在用户名中使用特殊字符'));
$validator->addRule('author', array($feedback, 'requireUserLogin'), _t('您所使用的用户名已经被注册,请登录后再次提交'));
$validator->addRule('author', 'maxLength', _t('用户名最多包含200个字符'), 200);
if ($options->commentsRequireMail && !$user->hasLogin()) {
$validator->addRule('mail', 'required', _t('必须填写电子邮箱地址'));
}
$validator->addRule('mail', 'email', _t('邮箱地址不合法'));
$validator->addRule('mail', 'maxLength', _t('电子邮箱最多包含200个字符'), 200);
if ($options->commentsRequireURL && !$user->hasLogin()) {
$validator->addRule('url', 'required', _t('必须填写个人主页'));
}
$validator->addRule('url', 'url', _t('个人主页地址格式错误'));
$validator->addRule('url', 'maxLength', _t('个人主页地址最多包含200个字符'), 200);
$validator->addRule('text', 'required', _t('必须填写评论内容'));
$comment['text'] = $self->request->text;
/** 对一般匿名访问者,将用户数据保存一个月 */
if (!$user->hasLogin()) {
/** Anti-XSS */
$comment['author'] = $self->request->filter('trim')->author;
$comment['mail'] = $self->request->filter('trim')->mail;
$comment['url'] = $self->request->filter('trim')->url;
/** 修正用户提交的url */
if (!empty($comment['url'])) {
$urlParams = parse_url($comment['url']);
if (!isset($urlParams['scheme'])) {
$comment['url'] = 'http://' . $comment['url'];
}
}
$expire = $options->gmtTime + $options->timezone + 30 * 24 * 3600;
Typecho_Cookie::set('__typecho_remember_author', $comment['author'], $expire);
Typecho_Cookie::set('__typecho_remember_mail', $comment['mail'], $expire);
Typecho_Cookie::set('__typecho_remember_url', $comment['url'], $expire);
} else {
$comment['author'] = $user->screenName;
$comment['mail'] = $user->mail;
$comment['url'] = $user->url;
/** 记录登录用户的id */
$comment['authorId'] = $user->uid;
}
/** 如果系统设置为不需要审核,并且评论者之前须有评论通过了审核 */
if (!$options->commentsRequireModeration && $options->commentsWhitelist) {
if ($feedback->size($feedback->select()->where('author = ? AND mail = ? AND status = ?', $comment['author'], $comment['mail'], 'approved'))) {
$comment['status'] = 'approved'; // 评论者之前有评论通过了审核设置为审核通过
} else {
$comment['status'] = 'waiting'; // 没有的话评论设置为待审核
}
}
if ($error = $validator->run($comment)) {
$self->response->throwJson(array('status' => 0, 'msg' => implode(';', $error)));
}
/** 添加评论 */
$commentId = $feedback->insert($comment);
if (!$commentId) {
$self->response->throwJson(array('status' => 0, 'msg' => _t('评论失败')));
}
Typecho_Cookie::delete('__typecho_remember_text');
$db->fetchRow($feedback->select()->where('coid = ?', $commentId)
->limit(1), array($feedback, 'push'));
// 返回评论数据
$data = array(
'cid' => $feedback->cid,
'coid' => $feedback->coid,
'parent' => $feedback->parent,
'mail' => $feedback->mail,
'url' => $feedback->url,
'ip' => $feedback->ip,
'agent' => $feedback->agent,
'author' => $feedback->author,
'authorId' => $feedback->authorId,
'permalink' => $feedback->permalink,
'created' => $feedback->created,
'datetime' => $feedback->date->format('Y-m-d H:i:s'),
'status' => $feedback->status,
);
// 评论内容
ob_start();
$feedback->content();
$data['content'] = ob_get_clean();
$data['avatar'] = Typecho_Common::gravatarUrl($data['mail'], 48, Helper::options()->commentsAvatarRating, NULL, $self->request->isSecure());
$self->response->throwJson(array('status' => 1, 'comment' => $data));
}
此处代码取自icefox主题地址:https://github.com/xiaopanglian/icefox/
#免责声明#
本文为转载 或 原创内容,未经授权禁止转载、摘编、复制及镜像使用、转载请注明作者、出处及原文链接、违者将依法追究责任。

8个月前 最近用独立页面编写的一个朋友圈模板,基于评论功能实现。 为了实现显...

7个月前 博主在制作主题的时候遇到开启pjax功能后,在文章中进行评论会出现无法评...

2年前以前使用的方法还是太嫩了,最近才发现如果评论存在分页后就会导致评论无...

8个月前 近期有网友反映开启pjax无刷新功能后,在文章评论区域的退出功能会发生失...

7个月前 最近用独立页面编写的一个朋友圈模板,基于评论功能实现。 为了实现显...

7个月前 > 首先声明,下面的代码基本都是用AI写作,博主只是辅助提供意见修改。...
cid,6) as $comment): ?>
authorId) { ?>作者
authorId);?>:
|
|<\/p>/", ' ', $comment['text'])) ?>