使用phpcms调用全站最新文章方法

来自:互联网
时间:2019-05-04
阅读:

这是本次改版的需要,网上虽然有这个教程,但我写文章的目的在于再次有这个需求时不用去找自己的源码复制或去百度找教程,而是直接看自己写的文章。
方法如下:
打开/phpcms/modules/content/classes/content_tag.class.php在最后一个“}”之前粘贴如下代码:

public function newcontent($data){ 
$num = intval($data['limit']) ? intval($data['limit']) : '20'; 
// 设置排序 
Switch($data['order']){ 
case '1': 
$order = ' `id` ASC '; 
break; 
case '2': 
$order = ' `id` DESC '; 
break; 
case '3': 
$order = ' `inputtime` ASC '; 
break; 
case '4': 
$order = ' `inputtime` DESC '; 
break; 
case '5': 
$order = ' `updatetime` ASC '; 
break; 
case '6': 
$order = ' `updatetime` DESC '; 
break; 
default: 
$order = ' `id` DESC '; 
} 
if($data['catid']){ 
$catids = explode(',', $data['catid']); 
foreach($catids as $catid){ 
$catid = intval($catid); 
if(emptyempty($catid))continue; 
$this->set_modelid($catid); 
$where = $this->category[$catid]['child'] ? ' `catid` IN ('.$this->category[$catid]['arrchildid'].')' : " `catid` = $catid"; 
$datas = $this->db->select($where, '*', $num, $order); 
$data[$catid]['data'] = $datas; 
// 记录本次的文章数 
$data['num'][] = count($datas); 
$model_num++; 
} 
}else{ 
$models = getcache('model', 'commons'); 
foreach($models as $model){ 
$this->db->set_model($model['modelid']); 
$datas = $this->db->select('', '*', $num, $order); 
$data[$model['modelid']]['data'] = $datas; 
// 记录本次的文章数 
$data['num'][] = count($datas); 
$model_num++; 
} 
} 
if($data){ 
// 获取每个模型应该截取的条数 
$num = ceil($num/$model_num); 
// 循环条数记录用于找出条数不满足的数量然后进行平均 
$w_num = $w_num_t = ''; 
foreach($data['num'] as $num_t){ 
if($num_t < $num){ 
$w_num += $num-$num_t; 
$w_num_t++; 
} 
} 
// 判断是否有不满足平均数的 如果有那么就增加平均值 
if($w_num_t){ 
$num += ceil($w_num/($model_num-$w_num_t)); 
} 
$datas = array(); 
foreach($data as $r){ 
$r_n = ''; 
if(is_array($r['data'])) 
foreach($r['data'] as $r_t){ 
$datas[] = $r_t; 
if(++$r_n == $num)break; 
} 
} 
return $datas; 
}else{ 
return false; 
} 
}

下面是我首页“今日更新”模块调用全站最新文章的代码(我还调用了点击量)

{pc:content action="newcontent" num="1"} 
{php $categorys = getcache('category_content_'.$siteid,'commons');} 
<ul> 
{loop $data $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));} 
<li> 
<h2><span class="todayupdate">今日更新</span><a href="{$v['url']}" target=_blank>{str_cut($v[title],200,”)}</a><span class="ydcs">(浏览&nbsp;:&nbsp;{$views})</span></h2> 
<p class="description">{str_cut($v[description],500)}</p> 
</li> 
{/loop} 
</ul> 
{/pc}

如果你不想调用点击量,那么代码如下:

{pc:content action="newcontent" num="1"} 
<ul> 
{loop $data $v} 
<li> 
<h2><span class="todayupdate">今日更新</span><a href="{$v['url']}" target=_blank>{str_cut($v[title],200,”)}</a><span class="ydcs">(浏览&nbsp;:&nbsp;{$views})</span></h2> 
<p class="description">{str_cut($v[description],500)}</p> 
</li> 
{/loop} 
</ul> 
{/pc}

大家可参考调用。

返回顶部
顶部