规则之树

typecho调用用户文章总数及评论总数

这也是本博客模板需要实现的功能,各位可以在阅读文章的页面发现文章总数及评论总数,不过还想要进一步的实现功能就还得写更多代码了,我所利用的是通过作者ID对数据库的查询,和之前的最近文章列表的原理基本相同,不过这次输出的不是数组而已。主要参考“博客吧”的教程,并且在它们的基础上自己变通了一点点。

获取文章总数代码如下:

/**输出作者文章总数,可以指定*/
function allpostnum($id){
    $db = Typecho_Db::get();
    $postnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'allpostnum'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
    $postnum = $postnum['allpostnum'];
    return $postnum;
}

获取评论总数代码如下:

/**输出作者评论总数,可以指定*/
function commentnum($id){
    $db = Typecho_Db::get();
    $commentnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'commentnum'))->from ('table.comments')->where ('table.comments.authorId=?',$id)->where('table.comments.type=?', 'comment'));
    $commentnum = $commentnum['commentnum'];
    return $commentnum;
}

可以看到就是很基本的数据库查询,变通起来也是相当容易。
效果如下:

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »