twcms默认首页、频道页面、列表页不带浏览数显示功能,内容也浏览数存在刷浏览数风险,以下为二开方法。
如下效果展示。
下面开始修改
一、博客首页修改,
1、打开block目录下kp_block_global_blog.lib.php文件在
$orderway=isset($conf['orderway'])&&$conf['orderway']==1?1:-1;
代码下面增加
$showviews=_int($conf,'showviews',0);
$list_arr=$run->cms_content->list_arr(array(),'id',-1,($page-1)*$pagenum,$pagenum,$total);
代码下面增加
if($showviews){ $run->cms_content_views->table='cms_'.$table.'_views'; $keys=array(); foreach($list_arras$v){ $keys[]=$v['id']; } $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$table.'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
$run->cms_content->format($v,$mid,$dateformat,$titlenum,$intronum); //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
在首页模板标签增加showviews="1" 浏览量显示开关
{block:global_blogmid="2"pagenum="10"intronum="130"showviews="1"}
即可使用{$v[views]} 调用浏览数。
二、频道页面更改
1、打开block目录下kp_block_listeach.lib.php文件在
$limit=_int($conf,'limit',10); //后面增加如下代码 $showviews=_int($conf,'showviews',0);
2、增加代码
$ret[$_cid]['list']=$run->cms_content->find_fetch($where,array($orderby=>$orderway),0,$limit); 代码下面增加 if($showviews){ $keys=array(); foreach($ret[$_cid]['list']as$lv){ $keys[]=$lv['id']; } $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$table.'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
3、增加代码
$run->cms_content->format($v,$mid,$dateformat,$titlenum,$intronum); //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
即可完成频道页面调用,调用方法如第一条。
三、列表页面修改
1、打开block目录下kp_block_list.lib.php
$limit=_int($conf,'limit',10); //下面增加 $showviews=_int($conf,'showviews',0);
2、增加代码
$list_arr=$run->cms_content->find_fetch($where,array($orderby=>$orderway),$start,$limit); //下面增加 if($showviews){ $run->cms_content_views->table='cms_'.$table.'_views'; $keys=array(); foreach($list_arras$v){ $keys[]=$v['id']; } $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$table.'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
3、增加代码
$run->cms_content->format($v,$mid,$dateformat,$titlenum,$intronum); //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
完成。
4、打开打开block目录下kp_block_global_cate.lib.php
$orderway=isset($conf['orderway'])&&$conf['orderway']==1?1:-1; //下面增加 $showviews=_int($conf,'showviews',0);
5、代码增加
$list_arr=$run->cms_content->list_arr($where,$orderby,$orderway,($page-1)*$pagenum,$pagenum,$total); //下面增加 if($showviews){ $run->cms_content_views->table='cms_'.$run->_var['table'].'_views'; $keys=array(); foreach($list_arras$v){ $keys[]=$v['id']; } $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$run->_var['table'].'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
6、
$v['cate_url']=$run->category->category_url($cate_arr[$v['cid']]['cid'],$cate_arr[$v['cid']]['alias']); } //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
四、内容页面修改
1、打开block目录下kp_block_global_show.lib.php
if($data)$_show+=$data; //下面增加 $run->cms_content_views->table='cms_'.$run->_var['table'].'_views'; $views_data=$run->cms_content_views->get($id); if($views_data){ $_show['views']=$views_data['views']+1; $run->cms_content_views->set($id,array('views'=>$_show['views'],'cid'=>$_show['cid'])); }else{ $_show['views']=1; $run->cms_content_views->set($id,array('views'=>1,'cid'=>$_show['cid'])); }
五、其他调用标签增加
1、打开block目录下kp_block_list_flag.lib.php
$limit=_int($conf,'limit',10); //下面增加 $showviews=_int($conf,'showviews',0);
$keys[]=$v['id']; } //下面增加 if($showviews){ $run->cms_content_views->table='cms_'.$table.'_views'; $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$table.'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
$run->cms_content->format($v,$mid,$dateformat,$titlenum,$intronum); //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
kp_block_taglike.lib.php
$limit=_int($conf,'limit',10); //下面增加 $showviews=_int($conf,'showviews',0);
foreach($tag_arras$v){ $keys[]=$v['id']; } //下面增加 if($showviews){ $run->cms_content_views->table='cms_'.$table.'_views'; $views_list_arr=$run->cms_content_views->mget($keys); $views_key='cms_'.$table.'_views-id-'; }else{ $views_key=''; $views_list_arr=array(); }
$run->cms_content->format($v,$mid,$dateformat,$titlenum,$intronum); //下面增加 if($showviews&&$views_list_arr){ $v['views']=isset($views_list_arr[$views_key.$v['id']])?$views_list_arr[$views_key.$v['id']]['views']:0; }
六、打开control/views_control.class.php
注释掉
/*publicfunctionindex(){ $_ENV['_config']['cache']['l2_cache']=0; $id=(int)R('id'); $cid=(int)R('cid'); $_var=$this->category->get_cache($cid); empty($_var)&&core::error404(); $mviews=&$this->models->cms_content_views; $mviews->table='cms_'.$_var['table'].'_views'; $key=$mviews->arr2key($id); $data=$mviews->db->get($key); if(!$data)$data=array('id'=>$id,'cid'=>$cid,'views'=>0); $data['views']++; echo'varviews='.$data['views'].';'; $mviews->db->set($key,$data); exit;*/
完成。