
第一、随机文章代码
这个代码直接添加到当前主题的Functions.php文件中。本来还是有一个随机插件的,这里我考虑到需要用到当天的时间所以不去用插件。
- function getRandomPosts($limit = 10){
- $db = Typecho_Db::get();
- $result = $db->fetchAll($db->select()->from('table.contents')
- ->where('status = ?','publish')
- ->where('type = ?', 'post')
- ->where('created <= unix_timestamp(now())', 'post')
- ->limit($limit)
- ->order('RAND()')
- );
- if($result){
- $i=1;
- foreach($result as $val){
- if($i<=3){
- $var = ' class="red"';
- }else{
- $var = '';
- }
- $val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
- $post_title = htmlspecialchars($val['title']);
- $permalink = $val['permalink'];
- echo '<li><i'.$var.'>'.$i.'</i><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';
- $i++;
- }
- }
- }
第二、调用代码
- <?php getRandomPosts('10');?>
在需要调用的位置调用随机文章。这里在文章标题中是没有时间的,我们可以在第一步的代码里加上时间,直接用date("Y/m/d")调用就可以了。
本文来自投稿,不代表本站立场,如若转载,请注明出处: