Typecho 随机文章(日志)

Typecho 随机文章调用代码,使用这个代码省的用插件了。
博主作为一个有洁癖的人,做模板能不用插件就尽量不用。

此代码加在functions.php

function theme_random_posts(){

$defaults = array(

'number' => 5,
'before' => '<ul class="list">',
'after' => '</ul>',

'xformat' => '<li><a href="{permalink}" title="{title}">{title}</a></li>'
);
$db = Typecho_Db::get();
 
$sql = $db->select()->from('table.contents')
->where('status = ?','publish')
->where('type = ?', 'post')
->where('created <= unix_timestamp(now())', 'post') //添加这一句避免未达到时间的文章提前曝光
->limit($defaults['number'])
->order('RAND()');
 
$result = $db->fetchAll($sql);
echo $defaults['before'];
foreach($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($val);
echo str_replace(array('{permalink}', '{title}'),array($val['permalink'], $val['title']), $defaults['xformat']);
}
echo $defaults['after'];
}

调用代码:

<?php theme_random_posts();?>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/9
1 条评论
3.2k

发表评论

仅有一条评论

  1. 校长不让说     Win 7 /    Chrome
    2020-07-21 22:57

    :不高兴::不高兴::不高兴::不高兴:

!