首页 » 遗忘 » 正文内容
1年前 寻梦xunm 295 遗忘

下面的代码都是编写成函数,放在主题的 functions.php 文件中,在每个页面都能调用。
获取文章数量

下面是获取文章总数量的函数,不包含独立页面:

function postCount() {
    $db = Typecho_Db::get();
    // 查询出文章数量并转换为数组
    $count = $db->fetchRow($db->select('COUNT(*)')->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish'));
    // 返回文章数量
    return $count['COUNT(*)'];
}

上面的函数可以获取文章数量,不包含草稿和未公开的文章。

Typecho 的文章和独立页面都存储在 Typecho 数据库的 typecho_contents 表中,使用 Typecho 提供的 API 查询时可以不需要加 typecho 前缀。

typecho_contents 表中有个 type 字段是用来存储内容类型的,post 是普通文章,page 是独立页面。status 字段是存储文章状态,publish 是公开的,draft 是草稿。

如果要获取独立页面数量可以把 where('type = ?', 'post') 换为 where('type = ?', 'page') ,如果要获取所有公开、未公开、和草稿文章数量可以直接把 where('status = ?', 'publish') 方法删除。
获取评论数量

下面是获取评论总数量的函数,只包含通过审核的评论:

function commentsCount() {
    $db = Typecho_Db::get();
    // 查询出评论数量并转换为数组
    $count = $db->fetchRow($db->select('COUNT(*)')->from('table.comments')->where('status = ?', 'approved'));
    // 返回评论数量
    return $count['COUNT(*)'];
}

Typecho 的评论存储在 typecho_comments 表中,status 字段存储评论的状态,approved 是通过审核的评论,waiting 是待审核的评论,spam 是标记为垃圾的评论。

如果要获取所有评论,不区分状态的话可以把 where('status = ?', 'approved') 方法删除。
获取分类数量

下面是获取分类数量的函数:

function categoryCount() {
    $db = Typecho_Db::get();
    // 查询出分类数量并转换为数组
    $count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'category'));
    // 返回分类数量
    return $count['COUNT(*)'];
}

Typecho 的分类和标签都存储在 typecho_metas 表中,这个表中有一个 type 字段用来区分标签和分类,tag 是标签,category 是分类。
获取标签数量

下面是获取标签数量的函数

function tagCount() {
    $db = Typecho_Db::get();
    $count = $db->fetchRow($db->select('COUNT(*)')->from('table.metas')->where('type = ?', 'tag'));
    return $count['COUNT(*)'];
}

Typecho 的分类和标签都存储在一个表中,只需要把 where('type = ?', 'category') 换为 where('type = ?', 'tag') 就可以查询出标签数量。
获取总点赞数量

Typecho 是不包含点赞功能的,如果你开发了点赞功能,在文章表中加入了 agree 字段的话,可以使用下面的函数获取点赞总数:

function agreeCount() {
    $db = Typecho_Db::get();
    // 对文章表的 agree 字段进行求和并把查询出的结果转换为数组
    $count = $db->fetchRow($db->select('SUM(agree) AS agreeCount')->from('table.contents'));
    // 返回总点赞数
    return $count['agreeCount'];
}

获取评论数量排名前 5 的 5 篇文章

下面是获取评论数排名前 5 的 5 篇文章的函数,除了文章外也会获取评论数:

function top5CommentPost() {
    $db = Typecho_Db::get();
    // 查询出 5 条评论数最大的文章,按照评论数从大到小排序,然后把查询出的数据转换为数组
    $top5Post = $db->fetchAll($db->select()->from('table.contents')->where('type = ?', 'post')->where('status = ?', 'publish')->order('commentsNum', Typecho_Db::SORT_DESC)->offset(0)->limit(5));
    // 用来存储文章信息的数组
    $postList = array();
    foreach ($top5Post as $post) {
        // 使用 Typecho 提供的 API 获取每篇文章的链接地址
        $post = Typecho_Widget::widget('Widget_Abstract_Contents')->filter($post);
        // 把文章标题、链接、点赞数加入数组
        array_push($postList, array(
            'title' => $post['title'],
            'link' => $post['permalink'],
            'commentsNum' => $post['commentsNum']
        ));
    }
    // 返回 5 篇文章的标题、链接、点赞数
    return $postList;
}

在 typecho_contents 文章表中有一个 commentsNum 字段就是用来存储评论数量的。

上面的 top5CommentPost 函数返回的数据如下:

Array
(
    [0] => Array
        (
            [title] => MWordStar 响应式双栏 Typecho 博客主题
            [link] => http://localhost/php/typecho/index.php/archives/812/
            [commentsNum] => 159
        )

    [1] => Array
        (
            [title] => MWordStar 使用说明
            [link] => http://localhost/php/typecho/index.php/archives/819/
            [commentsNum] => 127
        )

    [2] => Array
        (
            [title] => 红米 AC2100 刷入 Padavan 固件
            [link] => http://localhost/php/typecho/index.php/archives/883/
            [commentsNum] => 38
        )

    [3] => Array
        (
            [title] => Facile 一个简洁的 Typecho 博客主题
            [link] => http://localhost/php/typecho/index.php/archives/899/
            [commentsNum] => 27
        )

    [4] => Array
        (
            [title] => Linux 搭建 Minecraft 基岩版服务器
            [link] => http://localhost/php/typecho/index.php/archives/865/
            [commentsNum] => 26
        )

)

获取各分类的文章数量

下面是获取分类文章数量的函数,可以用来生成统计图表:

function categoryPostCount() {
    $db = Typecho_Db::get();
    // 查询出每个分类的文章数量并转换为数组
    $count = $db->fetchAll($db->select('name', 'count')->from('table.metas')->where('type = ?', 'category'));
    // 如果没有查询到数据就返回空数组
    if (count($count) < 1) {
        return array();
    }
    // 返回每个分类的文章数量
    return $count;
}

分类文章数量也是存储在 typecho_metas 表中,count 字段就是分类或标签的文章数量。

下面是获取的数据:

Array
(
    [0] => Array
        (
            [name] => PHP
            [count] => 14
        )

    [1] => Array
        (
            [name] => 前端开发
            [count] => 69
        )

    [2] => Array
        (
            [name] => 数据库
            [count] => 6
        )

    [3] => Array
        (
            [name] => Linux相关
            [count] => 23
        )

    [4] => Array
        (
            [name] => 实用教程
            [count] => 28
        )

)

文章来源于:https://www.veidc.com/23884.html

0 赞 or 打赏
喜欢就打赏一点
微信 支付宝
已有2 条评论,0 个点赞
  1. 阿度的头像
    阿度

    1年前 . LV.1

    按照大佬的方法已使用获取全站点赞方法,赞一个。

    Android Chrome 湖南省永州市
    1. 寻梦xunm的头像
      寻梦xunm 作者博主

      1年前 . LV.0

      @阿度

      前辈贡献出来的代码,使用起来确实方便。

      Android 夸克浏览器 重庆市
隐私

Q Q:1340326824

vipshiyi@qq.com

QQ群:422720328

生活所迫,再见了苦逼的BUG们,以后有缘在会吧。