這是本次改版的需要,網(wǎng)上雖然有這個(gè)教程,但我寫文章的目的在于再次有這個(gè)需求時(shí)不用去找自己的源碼復(fù)制或去百度找教程,而是直接看自己寫的文章。
方法如下:
打開(kāi)/phpcms/modules/content/classes/content_tag.class.php在最后一個(gè)“}”之前粘貼如下代碼:
public function newcontent($data){ $num = intval($data['limit']) ? intval($data['limit']) : '20'; // 設(shè)置排序 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; // 記錄本次的文章數(shù) $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; // 記錄本次的文章數(shù) $data['num'][] = count($datas); $model_num++; } } if($data){ // 獲取每個(gè)模型應(yīng)該截取的條數(shù) $num = ceil($num/$model_num); // 循環(huán)條數(shù)記錄用于找出條數(shù)不滿足的數(shù)量然后進(jìn)行平均 $w_num = $w_num_t = ''; foreach($data['num'] as $num_t){ if($num_t < $num){ $w_num += $num-$num_t; $w_num_t++; } } // 判斷是否有不滿足平均數(shù)的 如果有那么就增加平均值 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; } }
下面是我首頁(yè)“今日更新”模塊調(diào)用全站最新文章的代碼(我還調(diào)用了點(diǎn)擊量)
{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">(瀏覽 : {$views})</span></h2> <p class="description">{str_cut($v[description],500)}</p> </li> {/loop} </ul> {/pc}
如果你不想調(diào)用點(diǎn)擊量,那么代碼如下:
{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">(瀏覽 : {$views})</span></h2> <p class="description">{str_cut($v[description],500)}</p> </li> {/loop} </ul> {/pc}
大家可參考調(diào)用。
版權(quán)聲明: 本站資源均來(lái)自互聯(lián)網(wǎng)或會(huì)員發(fā)布,如果侵犯了您的權(quán)益請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)刪除!謝謝!
轉(zhuǎn)載請(qǐng)注明: 使用phpcms調(diào)用全站最新文章方法