Typecho article call
When the Typecho program designs themes, the sidebar sometimes needs to call popular articles or the latest articles.
We can call it directly through the script at the specified location.
In this article, we will organize this method of calling articles, which can be directly called and used in templates where needed in the future.
In fact, designing a theme is just that. After the static template is completed, it is called directly.
Latest article call
<?php
$this->widget('Widget_Contents_Post_Recent','pageSize=10')->to($recent);
if($recent->have()):
while($recent->next()):
?>
<li><a href="<?php $recent->permalink();?>"><?php $recent->title();?></a></li>
<?php endwhile; endif;?>Call the latest 10 articles and modify the value as needed.
Call popular articles
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>';
}
}
}Added in current theme Functions.php.
<?php getHotComments('10');?>The call appears where needed.