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

在functions.php中加入如下函数

  1. function allpostnum($id){
  2. $db = Typecho_Db::get();
  3. $postnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'allpostnum'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
  4. $postnum = $postnum['allpostnum'];
  5. return $postnum;
  6. }

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

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

代码解析

  1. $db = Typecho_Db::get();
  2. //获取数据库
  3. $postnum=$db->fetchRow($db->select(array('COUNT(authorId)'=>'allpostnum'))->from ('table.contents')->where ('table.contents.authorId=?',$id)->where('table.contents.type=?', 'post'));
  4. //按条件查询数据,将符合结果的条数生成一个数组
  5. //print_r($postnum);取消注释可调试显示结果
  6. $postnum = $postnum['allpostnum'];
  7. //获取作者文章总数量
  8. return $postnum;
  9. //返回作者文章总数量,用return能够更加方便的处理事情
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/57
0 评论
3.2k

发表评论

!