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能够更加方便的处理事情
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/57
0 评论
2.7k

发表评论

!