菜鸟站长
个人博客主题模板、应用插件、功能开发技术资源聚合分享网站

Typecho 获取当前用户的评论数及文章数

发布者:菜鸟站长  发布日期:2023-12-27  更新日期:2023-12-27  人气指数:159

Typecho 获取当前用户的评论数及文章数,先把这段代码扔到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(); // 输出该用户的评论数量
?>
本文检索关键词:typecho教程 , 文章数量 , 评论数量

菜鸟站长推荐教程



添加新评论 »

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png