我们在二次开发Typecho程序主题的时候,会较多用到的文章调用,比如最新文章、最新文章、热门文章的调用,这里我整理代码如下。
最新文章:
<?php $this->widget('Widget_Contents_Post_Recent')->to($post); ?>
<?php while($post->next()): ?>
<a href=”<?php $post->permalink(); ?>” title=”<?php $post->title(); ?>”>
<?php $post->title(25, '…'); ?></a>
<?php endwhile; ?>
热门文章调用:
// 热门文章调用 by laozuo.org
function getHotComments($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('commentsNum', Typecho_Db::SORT_DESC)
);
if($result){
foreach($result as $val){
$val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
$post_title = htmlspecialchars($val['title']);
$permalink = $val['permalink'];
echo '<li><a href="'.$permalink.'" title="'.$post_title.'" target="_blank">'.$post_title.'</a></li>';
}
}
}
调用:
<?php getHotComments('10');?>
相关文章调用:
<?php $this->related(5)->to($relatedPosts); ?>
<ul>
<?php while ($relatedPosts->next()): ?>
<li><a href="<?php%20$relatedPosts->permalink();%20?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
<?php endwhile; ?>
</ul>