Typecho随机调用文章,减轻模板页面的体积
洛书2020-11-11
今天在帮助客户调整一个企业网站主题的时候发现他的程序是用的Typecho,但是侧栏的随机文章是使用的直接SQL代码调用的,感觉比较麻烦,于是我准备寻找一款直接定义函数的调用,这样就可以减少主题页面模板的代码体积,本身我们在使用Typecho就要考虑到轻便。
- 废话不多说,我们将代码丢到当前主题的Functions.php系统文件中
//随机文章调用 itbulu.com 整理网络 function getRandomPosts($limit = 9){ $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('8');?>
版权声明:本文由洛书文章网发布,转载请注明本文链接!
本文内容如若侵权请您联系邮箱:1553396808@qq.com
推荐阅读