多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Phpcms V9 各类页面调用文章点击量及评论数量方法 ![](https://csdnimg.cn/release/phoenix/template/new_img/reprint.png) [大海哪蓝](https://me.csdn.net/wang178yang)2017-12-13 16:07:25![](https://csdnimg.cn/release/phoenix/template/new_img/articleReadEyes.png)951![](https://csdnimg.cn/release/phoenix/template/new_img/tobarCollect.png)收藏 分类专栏:[PHPCMS](https://blog.csdn.net/wang178yang/category_7211603.html) 一、[Phpcms](http://www.cmsyou.com/index.php?m=content&c=tag&a=lists&tag=Phpcms)列表页面取得文章点击量及评论数量: > {pc:content action="lists" catid="$catid" num="25" order="id DESC" page="$page" moreinfo="1"} {loop $data $r} {php $db = pc\_base::load\_model('hits\_model'); $\_r = $db->get\_one(array('hitsid'=>'c-'.$modelid.'-'.$r\[id\])); $views = $\_r\[views\]; } {php $comment\_tag = pc\_base::load\_app\_class("comment\_tag", "comment"); $comment\_total = $comment\_tag->count(array('commentid'=>'content\_'.$catid.'-'.$r\[id\].'-'.$modelid));} {date('Y-m-d H:i:s',$r\[inputtime\])}·{$r\[title\]} 点击:{$views} 评论数:{if $comment\_total}{$comment\_total}{else}0{/if}{/loop} {$pages} {/pc} 二、[Phpcms](http://www.cmsyou.com/index.php?m=content&c=tag&a=lists&tag=Phpcms)频道页面取得文章点击量及评论数量: > {pc:content action="lists" catid="$v\[catid\]" num="5" order="id DESC"} {loop $data $v} {php $db = pc\_base::load\_model('hits\_model'); $\_r = $db->get\_one(array('hitsid'=>'c-'.$modelid.'-'.$v\[id\])); $views = $\_r\[views\]; } {php $comment\_tag = pc\_base::load\_app\_class("comment\_tag", "comment"); $comment\_total = $comment\_tag->count(array('commentid'=>'content\_'.$v\[catid\].'-'.$v\[id\].'-'.$modelid));} {$v\[title\]}点击:{$views}评论数:{if $comment\_total}{$comment\_total}{else}0{/if} {/loop} {/pc} 三、Phpcms首页面取得文章点击量及评论数量: > {pc:content action="lists" catid="$r\[catid\]" num="5" order="id DESC" return="info"} {php $categorys = getcache('category\_content\_'.$siteid,'commons');} {loop $info $v} {php $category = $categorys\[$v\[catid\]\];} {php $modelid = $category\['modelid'\];} {php $db = pc\_base::load\_model('hits\_model'); $\_r = $db->get\_one(array('hitsid'=>'c-'.$modelid.'-'.$v\[id\])); $views = $\_r\[views\]; } {php $comment\_tag = pc\_base::load\_app\_class("comment\_tag", "comment"); $comment\_total = $comment\_tag->count(array('commentid'=>'content\_'.$v\[catid\].'-'.$v\[id\].'-'.$modelid));} ·{str\_cut($v\['title'\],40)} 点击:{$views} 评论:{if $comment\_total}{$comment\_total}{else}0{/if} {/loop} {/pc} 四、Phpcms推荐位取得文章点击量及评论数量: > {pc:content action="position" posid="2" order="listorder DESC" num="4"} {php $categorys = getcache('category\_content\_'.$siteid,'commons');} {loop $data $r} {php $category = $categorys\[$r\[catid\]\];} {php $modelid = $category\['modelid'\];} {php $db = pc\_base::load\_model('hits\_model'); $\_r = $db->get\_one(array('hitsid'=>'c-'.$modelid.'-'.$r\[id\])); $views = $\_r\[views\]; } {php $comment\_tag = pc\_base::load\_app\_class("comment\_tag", "comment"); $comment\_total = $comment\_tag->count(array('commentid'=>'content\_'.$r\[catid\].'-'.$r\[id\].'-'.$modelid));} {str\_cut($r\[title\],36,'')} 点击:{$views} 评论:{if $comment\_total}{$comment\_total}{else}0{/if} {if $n==1}{/if}{str\_cut($r\[description\],112)} {/loop} {/pc} 单独写在扩展函数phpcms\\libs\\functions\\extention.func.php中,然后在模板里面调用该函数,分如下两步: 一、在系统扩展函数文件phpcms\\libs\\functions\\extention.func.php中新增GetHitsFromId函数: /\*根据模型ID和信息ID获取点击数\*/ function GetHitsFromId($modelid,$id){   $hitdb = pc\_base::load\_model('hits\_model');   $\_r = $hitdb->get\_one(array('hitsid'=>'c-'.$modelid.'-'.$id));   $views = $\_r\['views'\];   if($views){     return $views;   }else{     return 0;   } } 二、模板中调用如下(第一个参数1代表模型ID,根据自己的当前模型来写;$r\['id'\])为文章ID): 浏览次数: {GetHitsFromId(1,$r\['id'\])}