把下面代码放到functions.php
class XAuthor extends Widget_Abstract_Users
{
protected $options;
/**
* 构造函数,初始化组件
*
* @param mixed $request
* @param mixed $response
* @param mixed $params
* @throws Typecho_Widget_Exception
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
$options = $this->widget('Widget_Options');
}
/**
* 执行函数,初始化数据
*
* @access public
* @return void
*/
public function execute()
{
if ($this->parameter->uid) {
$this->db->fetchRow($this->select()
->where('uid = ?', $this->parameter->uid), array($this, 'push'));
}
}
/**
* 获取文章总数
*
* @param string
* @return void
*/
public function ___postsNum()
{
$args = func_get_args();
if (!$args) {
$args[] = '%d';
}
$select = $this->db->select('count(*) as num')->from("table.contents")->where('table.contents.type = ?', 'post')->where('table.contents.status = ? OR table.contents.status = ?', 'publish', 'hidden')->where('table.contents.authorId = ?', $this->parameter->uid)->limit(1);
$num = intval($this->db->fetchObject($select)->num);
echo sprintf(isset($args[$num]) ? $args[$num] : array_pop($args), $num);
}
/**
* 获取用户评论数
*
* @param string
* @return void
*/
public function commentsNum()
{
$args = func_get_args();
if (!$args) {
$args[] = '%d';
}
$num = intval($this->db->fetchObject($this->db->select(array('COUNT(cid)' => 'num'))->from('table.comments')->where('authorId = ?', $this->uid))->num);
echo sprintf(isset($args[$num]) ? $args[$num] : array_pop($args), $num);
}
}
在想调用的地方调用以下代码即可。
<?php
$uid = 1;
$author = Typecho_Widget::widget("XAuthor@" . $uid, "uid=" . $uid);
$author->postsNum(); // 输出该用户发表的文章数量
$author->commentsNum(); // 输出该用户的评论数量
?>