因为群友花花弄了个高仿掘金主题,没有实现热门与热评文章加载功能,特此为其分享点代码,先是简单弄了个json版本,后来根据需求又弄了个html的版本

使用

用法就是将下面的函数放到模板函数文件里,如果存在同名函数请自行合并。

JSON版

function themeInit($archive)
{
$db = Typecho_Db::get();//数据库操作调用
if($archive->request->api){
$pagesize=8;//每页文章数量
$p=1;if($archive->request->page){$p=$archive->request->page;}
$select=$db->select('cid')->from('table.contents')
            ->where('table.contents.status = ?','publish')
            ->where('table.contents.password IS NULL')
            ->where('table.contents.type = ?', 'post');
if($archive->request->api=='new'){           
$select=$select->order('table.contents.created', Typecho_Db::SORT_DESC);
}elseif($archive->request->api=='hot'){
$select=$select->order('table.contents.commentsNum', Typecho_Db::SORT_DESC);
}elseif($archive->request->api=='views'){
$select=$select->order('table.contents.views', Typecho_Db::SORT_DESC);
}  
$select=$db->fetchAll($select->page($p,$pagesize));//分页
$lon=count($select);
for($ii=0;$ii<$lon;$ii++){
$archive->widget('Widget_Archive@indexxiu', 'pageSize=1&type=post', 'cid='.$select[$ii]['cid'])->to($ji);
 $b[] = array( 
                "cid" => $ji->title,
                "title" => $ji->permalink,
                "description" => $ji->description,
            );   
}  
$archive->response->throwJson(array(
            'status' => '200',
            'data' => $b
        ));
}
}

html版本

function themeInit($archive)
{
$db = Typecho_Db::get();
if($archive->request->api){
$pagesize=8;//每页文章数量
$p=1;if($archive->request->page){$p=$archive->request->page;}
$nextpage=$p+1;
$select=$db->select('cid')->from('table.contents')
            ->where('table.contents.status = ?','publish')
            ->where('table.contents.password IS NULL')
            ->where('table.contents.type = ?', 'post');
if($archive->request->api=='new'){           
$select=$select->order('table.contents.created', Typecho_Db::SORT_DESC);
}elseif($archive->request->api=='hot'){
$select=$select->order('table.contents.commentsNum', Typecho_Db::SORT_DESC);
}elseif($archive->request->api=='views'){
$select=$select->order('table.contents.views', Typecho_Db::SORT_DESC);
}  
$select=$db->fetchAll($select->page($p,$pagesize));//分页

$lon=count($select);
for($i=0;$i<$lon;$i++){
$archive->widget('Widget_Archive@indexxiu'.$i, 'pageSize=1&type=post', 'cid='.$select[$i]['cid'])->to($ji);
?>
<a class="title" href="<?php $ji->permalink() ?>" title="<?php $ji->title() ?>">
          <h2><?php $ji->title() ?></h2>
        </a>
<?php
if($i==$lon-1){
echo '<a class="next" href="'.Helper::options()->siteUrl.'?api='.$archive->request->api.'&page='.$nextpage.'">下页</a>';    
}
}  
exit;
}
}

调用方法

最新文章:https://你的域名/?api=new&page=1
热评文章:https://你的域名/?api=hot&page=1
热门文章:https://你的域名/?api=views&page=1
其中page为当前页码

PS

其实也可以利用我之前的https://blog.zezeshe.com/archives/typecho-custom-call.html 这个教程来写,应该能减少一些sql查询,不过我比较喜欢当前这个,主要是写起来比较方便!

版权属于:泽泽社长
本文链接:https://blog.zezeshe.com/archives/newhot-api-typecho.html
本站未注明转载的文章均为原创,并采用 CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!