昨天做的一個zblogPHP主題又用到了需要同時增加熱門文章、熱評論文、隨即文章功能。
一、可緩存、可分別設置條數、可分別指定分類的排行榜方法:
//排行榜 隨機 最新 熱門 留言 function ydblack_Get_Links( $type, $num, $tblogcate ) { global $zbp, $arrays, $order; //$str = ''; $str = ''; if ( $type == 'previous' ) { $order = array( 'log_PostTime' => 'DESC' ); } if ( $type == 'hot' ) { $order = array( 'log_ViewNums' => 'DESC' ); } if ( $type == 'comm' ) { $order = array( 'log_CommNums' => 'DESC' ); } if ( $zbp->db->type == "sqlite" ) { if ( $type == 'rand' ) { $order = array( 'random()' => '' ); } } else { if ( $type == 'rand' ) { $order = array( 'rand()' => '' ); } } $stime = time(); $ytime = ( 24 ) * 30 * 24 * 60 * 60; //時間可以控制 $ztime = $stime - $ytime; if ( empty( $tblogcate ) ) { $where = array( array( '=', 'log_Status', '0' ), array( '>', 'log_PostTime', $ztime ) ); } else { $where = array( array( '=', 'log_Status', '0' ), array( '>', 'log_PostTime', $ztime ), array( '=', 'log_CateID', $tblogcate ) ); } $str = $zbp->GetArticleList( array( '*' ), $where, $order, array( $num ), '' ); //注意 $str // if ( $type == 'rand' ) { //隨機 // foreach ( $array as $article ) { // $str .= ' // <li> // <h3><a href="'.$article->Url.'">'.$article->Title.'</a></h3> // <span><a href="'.$article->Author->Url.'">'.$article->Author->StaticName.'</a></span><i>'.$article->Time('Y-m-d').'</i> // </li> // '; // } // } return $str; } //隨機文章 ========================因為隨機不需要緩存,可以在模板中寫調用! //function ydblack_randpost() { // global $zbp; // $s = ''; // $array = ydblack_Get_Links( 'previous', '4', '1' ); // foreach ( $array as $article ) { // $s .= '<li><a href="' . $article->Url . '">' . $article->Title . '</a></li>'; // } // return $s; //} //生成 最新文章緩存 function ydblack_previouspost() { global $zbp; $s = ''; $array = ydblack_Get_Links( 'previous', '4', '1' ); foreach ( $array as $article ) { $s .= '<li><a href="' . $article->Url . '">' . $article->Title . '</a></li>'; } //$s .= '<!--您所查看的是ydblack 最新文章緩存,緩存時間'.date('Y-m-d h:i:sa',time()).'-->'; file_put_contents($zbp->usersdir . 'cache/ydblack_previouspost.txt', $s); return $s; } //獲取 最新文章緩存文件 function ydblack_previouspost_Cache(){ global $zbp; if (is_file($zbp->usersdir . 'cache/ydblack_previouspost.txt')==true){ $str = file_get_contents($zbp->usersdir . 'cache/ydblack_previouspost.txt'); }else{ $str = '<li>請重新提交下任意一篇文章,刪除也行,因本欄帶緩存!!!</li>'; } return $str; }
隨機部分寫在模板中的方法:
{$array = ydblack_Get_Links( 'rand', '4', '1' );} {foreach $array as $related} <li><h3><a href="{$article.Url}">{$article.Title}</a></h3><span><a href="{$article.Author.Url}">{$article.Author.StaticName}</a></span><i>{$article.Time('Y-m-d')}</i></li> {/foreach}
最新、熱門、留言發文/刪除時更新緩存方法:
掛:
Add_Filter_Plugin('Filter_Plugin_PostArticle_Succeed','ydblack_previouspost');//提交更新 Add_Filter_Plugin('Filter_Plugin_DelArticle_Succeed','ydblack_previouspost');//刪除更新
最新、熱門、留言調用代碼:
ydblack_previouspost_Cache()
二、不緩存、直接、簡單的調用方法
//hot comm rand function ydlinux_Get_Links($type,$num){ global $zbp,$str,$order; $str = ''; if($type=='previous'){ $order = array('log_PostTime'=>'DESC'); } if($type=='hot'){ $order = array('log_ViewNums'=>'DESC'); } if($type=='comm'){ $order = array('log_CommNums'=>'DESC'); } if ($zbp->db->type=="sqlite"){ if($type=='rand'){ $order = array('random()'=>''); } } else { if($type=='rand'){ $order = array('rand()'=>''); } } $where = array(array('=','log_Status','0')); $array = $zbp->GetArticleList(array('*'),$where,$order,array($num),''); foreach ($array as $key=>$article) { $i = $key+1; $str.= '<li class="sidelist'.$i.'"><a target="_blank" href="' .$article->Url. '">' .$article->Title. '</a></li>'; } return $str; } // function ydlinux_hotpost() { global $zbp; $s = ''; $s.= ydlinux_Get_Links('hot',"9"); return $s; } function ydlinux_commpost() { global $zbp; $s = ''; $s.= ydlinux_Get_Links('comm',"9"); return $s; } function ydlinux_randpost() { global $zbp; $s = ''; $s.= ydlinux_Get_Links('rand',"9"); return $s; }
下面分享出來代碼,是從zblogphp開發者@涂涂主題中復制過來:
//主函數 function tblog5_Get_Links($type,$num,$tblogcate,$tblogThumb){ global $zbp,$str,$order; $str = ''; if($type=='previous'){ $order = array('log_PostTime'=>'DESC'); } if($type=='hot'){ $order = array('log_ViewNums'=>'DESC'); } if($type=='comm'){ $order = array('log_CommNums'=>'DESC'); } if ($zbp->db->type=="sqlite"){ if($type=='rand'){ $order = array('random()'=>''); } } else { if($type=='rand'){ $order = array('rand()'=>''); } } $stime = time(); $ytime = ($zbp->Config('tblog5')->months)*30*24*60*60; $ztime = $stime-$ytime; if (empty($tblogcate)){ $where = array(array('=','log_Status','0'),array('>','log_PostTime',$ztime)); } else { $where = array(array('=','log_Status','0'),array('>','log_PostTime',$ztime),array('=','log_CateID',$tblogcate)); } $array = $zbp->GetArticleList(array('*'),$where,$order,array($num),''); foreach ($array as $article) { if ($tblogThumb=="1"){ $clsjtp=tblog5_firstimg($article); $str .='<a href="'.$article->Url.'" class="_ajx ease sb-border"><img width="160" height="130" src="'.$clsjtp.'" class="attachment-post-thumbnail wp-post-image" alt="'.$article->Title.'"><span class="publish">'.$article->Time('Y-m-d').'</span><span>'.$article->Title.'</span></a>'; } else{ $str .='<a href="'.$article->Url.'" class="_ajx ease sb-border sb_wt"><span class="publish">'.$article->Time('Y-m-d').'</span><span>'.$article->Title.'</span></a>'; } } return $str; } //緩存路徑 function tblog5_previouspost() { global $zbp; $s = ''; $s.= tblog5_Get_Links('previous',"5",$zbp->Config('tblog5')->previouscate,$zbp->Config('tblog5')->previousdisplay); $s .= '<!--您所查看的是tblog5 最新文章緩存,緩存時間'.date('Y-m-d h:i:sa',time()).'-->'; file_put_contents($zbp->usersdir . 'cache/tblog5_previouspost.txt', $s); } function tblog5_randpost() { global $zbp; $s = ''; $s.= tblog5_Get_Links('rand',"5",$zbp->Config('tblog5')->randcate,$zbp->Config('tblog5')->randdisplay); return $s; } function tblog5_hotpost() { global $zbp; $s = ''; $s.= tblog5_Get_Links('hot',"5",$zbp->Config('tblog5')->hotcate,$zbp->Config('tblog5')->hotdisplay); $s .= '<!--您所查看的是tblog5 熱門文章緩存,緩存時間'.date('Y-m-d h:i:sa',time()).'-->'; file_put_contents($zbp->usersdir . 'cache/tblog5_hotpost.txt', $s); } function tblog5_commentpost() { global $zbp; $s = ''; $s.= tblog5_Get_Links('comm',"5",$zbp->Config('tblog5')->commcate,$zbp->Config('tblog5')->commdisplay); $s .= '<!--您所查看的是tblog5 熱評文章緩存,緩存時間'.date('Y-m-d h:i:sa',time()).'-->'; file_put_contents($zbp->usersdir . 'cache/tblog5_commentpost.txt', $s); } //獲取路徑 function tblog5_previouspost_Cache(){ global $zbp; if (is_file($zbp->usersdir . 'cache/tblog5_previouspost.txt')==true){ $str=file_get_contents($zbp->usersdir . 'cache/tblog5_previouspost.txt'); }else{ $str='<li>請重新提交下任意一篇文章,刪除也行!!!</li>'; } return $str; } function tblog5_commentpost_Cache(){ global $zbp; if (is_file($zbp->usersdir . 'cache/tblog5_commentpost.txt')==true){ $str=file_get_contents($zbp->usersdir . 'cache/tblog5_commentpost.txt'); } else{ $str=tblog5_commentpost_Cache2(); } return $str; } function tblog5_commentpost_Cache2(){ global $zbp; $s = ''; $s.= tblog5_Get_Links('comm',5,$zbp->Config('tblog5')->commcate,$zbp->Config('tblog5')->commdisplay); return $s; } function tblog5_hotpost_Cache(){ global $zbp; if (is_file($zbp->usersdir . 'cache/tblog5_hotpost.txt')==true){ $str=file_get_contents($zbp->usersdir . 'cache/tblog5_hotpost.txt'); } else{ $str='<li>請留言一下</li>';} return $str; }
調用方法:
隨機文章:{php}echo tblog5_randpost(){/php}
熱評文章:{php}echo tblog5_commentpost_Cache(){/php}
熱門文章:{php}echo tblog5_hotpost_Cache(){/php}
最新文章:{php}echo tblog5_previouspost_Cache(){/php}