现在还处于静态的,那功能怎么实现呢?要知道,文章评论调用当前只能在内容详情页可用。
1,在后台系统里修改,配置参数 》会员配置里修改配置,评论功能开启,评论验证码关闭,评论审核关闭。
2,在ExtLabelController.php添加方法。
路径:/apps/home/controller/IndexController.php
作用:该文件的作用之一,是将已经写好的功能添加到对应的单页或列表页中。
修改:大约在249行,在“$content = $this->parser->parserAfter($content); // CMS公共标签后置解析”的下面添加新的功能。
// 列表
private function getListPage($sort)
{
// 调用栏目语言与当前语言不一致时,自动切换语言
if ($sort->acode != get_lg() && Config::get('lgautosw') !== '0') {
cookie('lg', $sort->acode);
}
if ($sort->listtpl) {
$this->checkPageLevel($sort->gcode, $sort->gtype, $sort->gnote);
$content = parent::parser($this->htmldir . $sort->listtpl); // 框架标签解析
$content = $this->parser->parserBefore($content); // CMS公共标签前置解析
$pagetitle = $sort->title ? "{sort:title}" : "{sort:name}"; // 页面标题
$content = str_replace('{pboot:pagetitle}', $this->config('list_title') ?: ($pagetitle . '-{pboot:sitetitle}-{pboot:sitesubtitle}'), $content);
$content = str_replace('{pboot:pagekeywords}', '{sort:keywords}', $content);
$content = str_replace('{pboot:pagedescription}', '{sort:description}', $content);
$content = $this->parser->parserPositionLabel($content, $sort->scode); // CMS当前位置标签解析
$content = $this->parser->parserSortLabel($content, $sort); // CMS分类信息标签解析
$content = $this->parser->parserListLabel($content, $sort->scode); // CMS分类列表标签解析
$content = $this->parser->parserAfter($content); // CMS公共标签后置解析
$content = $this->parser->parserCommentLabel($content); // 文章评论
} else {
error('请到后台设置分类栏目列表页模板!');
}
$this->cache($content, true);
}
添加的代码在该文件的其他地方也能看到:
$content = $this->parser->parserCommentLabel($content); // 文章评论
3,ExtLabelController.php添加新的方法,统计当前文章评论数。
路径:/apps/home/controller/ExtLabelController.php
修改:大约在35行,在“private function test()”的方法下面添加新的方法。
//获得评论数量private function getcommentrows()
{
$pattern = '/\{getcommentrows\s?\(([^\}]+)\)\}/'; if (preg_match($pattern, $this->content, $matches)) { $this->content = preg_replace_callback( $pattern, function($matches){ $extfield = $matches[1]; $result = \core\basic\Db::table('`ay_member_comment` a ')->field('count(*) as count') ->where("a.contentid='".$extfield."' AND pid = 0 AND a.status=1") ->find(); $value = $result->count; return $value;
}, $this->content);
}
}
然后在run()方法里面执行该方法
public function run($content)
{ // 接收数据
$this->content = $content;
// 执行个人自定义标签函数
$this->test();
//获得评论数
$this->getcommentrows();
// 返回数据
return $this->content;
}
4,在列表页模板上添加代码
{pboot:list}<div class="col-xs-12 pad0 {pboot:if('[list:n]'>'0')}mt15{/pboot:if}">
<div class="bottom mt15">
<form action="/comment/add/?contentid=[list:id]" method="post">
<div class="clear-fix flex fayan">
<input type="text" name="comment" id="comment" value="" placeholder="参与评论" />
<input type="submit" id="" name="" value="评论" />
</div>
</form>
<div class="pinglun">
{pboot:comment contentid=[list:id] page=0} <div class="plitem"><span>[comment:nickname] : </span>[comment:comment]</div>
{/pboot:comment} <div class="plitem"><a href=""><span>查看全部{getcommentrows([list:id])}条评论</span></a></div>
</div>
</div>
</div>{/pboot:list}
发布评论的表单是
<form action="/comment/add/?contentid=[list:id]" method="post">
<div class="clear-fix flex fayan">
<input type="text" name="comment" id="comment" value="" placeholder="参与评论" />
<input type="submit" id="" name="" value="评论" />
</div>
</form>
读取评论的表单是
{pboot:comment contentid=[list:id] page=0}<div class="plitem"><span>[comment:nickname] : </span>[comment:comment]</div>{/pboot:comment}
pboot:comment 标签需要参数page=0
最后多少条评论用{getcommentrows([list:id])}这个自定义的标签。
最后的效果不错,虽然功能还暂时不是很全,还可以拓展评论点赞,回复评论的功能。
原文地址:https://www.cnblogs.com/captain1024/p/15904501.html



