昨天做的一个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}