第一种方法
此问题需要添加一个标签,找到phpcms/modules/content/classes/content_tag.class.php,可以在最后添加这个function。
function sitehits($data){
if(empty($data['siteid'])) $data['siteid']=1;
$siteid = intval($data['siteid']);
$this->hits_db = pc_base::load_model('hits_model');
$category_content = getcache('category_content','commons');
$catid = '';
//获取站点下所有栏目ID
foreach($category_content as $key=>$val){
if($val==$siteid){
$catid .= $comma.$key;
$comma=',';
}
}
$sql='catid in('.$catid.')';
if(isset($data['day'])) {
$updatetime = SYS_TIME-intval($data['day'])*86400;
$sql .= " AND updatetime>'$updatetime'";
}
//获取点击排行
$r = $this->hits_db->select($sql,'hitsid',$data['limit'],$data['order']);
$return = array();
$sitemodel_model_db = pc_base::load_model('sitemodel_model');
$this->db_config = pc_base::load_config('database');
$tablepre = $this->db_config['default']['tablepre'];
foreach($r as $key){
preg_match_all('/-(d+)-/',$key['hitsid'],$modelid);
$id = substr($key['hitsid'],(strpos($key['hitsid'],'-',2)+1));
$tablename = $sitemodel_model_db->get_one(array('modelid'=>$modelid[1][0]),'tablename');
$this->db->table_name = $tablepre.$tablename['tablename'];
$return[] = array_merge($return,$this->db->get_one(array('id'=>$id)));
}
return $return;
}
这样就好了,下面看看调用的代码吧
{pc:content action="sitehits" siteid="1" num="10" day="7" order="views DESC" cache="3600"}
{loop $data $r}
<li><a href="{$r[url]}" target="_blank">{$r[title]}</a></li>
{/loop}
{/pc}
用get标签,直接去读取数据库,我读取了news,category这两上表,但他们有相同的变量名,所以用as将它改为新变量名,这样可以实现两个链接,分别链接到栏目所在的列表,和文章内容页。
用H标签而不用li标签,是为了增强SEO,加一个cache,这样可以减少数据库的压力。
{pc:get sql=”SELECT a.title, a.catid, b.catid, b.catname, a.url as turl ,b.url as curl, a.id FROM `v9_news` a, `v9_category` b WHERE a.catid = b.catid ORDER BY `a`.`id` DESC ” num=”12″ cache=”300″}
{loop $data $r}
<h5><font class=”cate”><a href=”{$r[curl]}”>[{$r[catname]}]</a></font><a class=”gray” href=”{$r[turl]}” title=”{$r[title]}”>{str_cut($r[‘title’],45)}</a></h5> {/loop}
{/pc}