typecho显示当前作者文章总数量

笔记
2627 0

在typecho的模板制作中,我们经常会用到作者文章的统计数量,当然这在个人博客中很容易实现,但在多用户中如要统计博主的文章数量又该如何实现呢,可以试试下面的方法:

在functions.php中加入如下函数

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;
}

显示当前作者文章总数量调用代码如下(适用于post.php,author.php,或者index.phpwhile循环中等等)

<?php echo allpostnum($this->author->uid); ?>

代码解析

$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'));
//按条件查询数据,将符合结果的条数生成一个数组
//print_r($postnum);取消注释可调试显示结果
$postnum = $postnum['allpostnum'];
//获取作者文章总数量
return $postnum;
//返回作者文章总数量,用return能够更加方便的处理事情
最后更新 2021-01-19
评论 ( 0 )
OωO
隐私评论