typecho的TePostViews插件

来源:https://typecho.show/archives/TePostViews.html

typecho支持文章阅读数,同时支持调用热门文章列表。

调用方法

// 输出访问次数
<?php _e('阅读:'); ?>(<?php $this->viewsNum(); ?>)

// 输出热门文章-默认方法
<section class="widget">
        <h3 class="widget-title"><?php _e('热门文章'); ?></h3>
        <ul class="widget-list">
            <?php TePostViews_Plugin::outputHotPosts() ?>
        </ul>
</section>

// 输出热门文章-我使用有效的方法(sqlite数据库)由GLM提供。

<section class="widget">
        <h3 class="widget-title"><?php _e('热门文章'); ?></h3>
        <ul class="widget-list">
                <?php
    // 定义热门文章数量
    $limit = 10;
        // 获取数据库连接
    $db = Typecho_Db::get();
    $options = Typecho_Widget::widget('Widget_Options');
    
    // 查询文章,按阅读量 viewsNum 降序排列
    // 注意:这里使用了 Typecho 标准的查询构造器,兼容 SQLite
    $posts = $db->fetchAll(
        $db->select()->from('table.contents')
             ->where('status = ?', 'publish')
             ->where('type = ?', 'post')
             ->order('viewsNum', Typecho_Db::SORT_DESC) // 关键:按插件生成的字段排序
             ->limit($limit)
    );
    
    // 输出列表
    if ($posts) {
        echo '<ul class="widget-list">';
        foreach ($posts as $post) {
            // 手动构建文章链接
            // Typecho_Router::url 会根据 slug 和路由规则生成正确的 URL
            $permalink = Typecho_Router::url('post', $post, $options->index);
            
            echo '<li><a href="' . $permalink . '">' . htmlspecialchars($post['title']) . '</a></li>';
        }
        echo '</ul>';
    }
    ?>
            </ul>
</section>

下载

https://qqdie.lanzouu.com/ir7Hm1j064lc

posted @ 2026-08-15 21:16  POTUS88  阅读(4)  评论(0)    收藏  举报