Typecho Wiki
每一个作品都值得被记录

Typecho实现今日更新文章数调用

Typecho维基君Typecho教程 • 128次浏览 • 发布 2024-04-07 • 更新 2024-04-07
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

在模板你要放的位置加上以下代码

<?php
$today = date('Y-m-d');
$nextDay = date('Y-m-d', strtotime('+1 day'));

$query = $this->db->select()->from('table.contents')
          ->where('created >= ?', strtotime($today))
          ->where('created < ?', strtotime($nextDay))
          ->where('type = ?', 'post')
          ->where('status = ?', 'publish');

$results = $this->db->fetchAll($query);
$postsToday = count($results);

echo "今天更新的文章数:" . $postsToday;
?>

你以为这就完了?

上面只是最基础的版本

下面是进阶版

打开functions.php 在最后加上下面这一段

function getTodayPostCount() {
    $today = date('Y-m-d');
    $nextDay = date('Y-m-d', strtotime('+1 day'));

    $query = Typecho_Db::get()->select()->from('table.contents')
              ->where('created >= ?', strtotime($today))
              ->where('created < ?', strtotime($nextDay))
              ->where('type = ?', 'post')
              ->where('status = ?', 'publish');

    $results = Typecho_Db::get()->fetchAll($query);
    $postsToday = count($results);

    return $postsToday;
}

然后在你要调用的地方加入 以下代码

<?php echo "今天更新的文章数:" . getTodayPostCount(); ?>

到了这里你以为就完了?当然不是 我还有高级版

还是在functions.php 加入以下代码,记得进阶和高级只能选一个。。。别2段代码都复制进去了

function getTodayPostCount() {
    $cacheFilePath = __DIR__ . '/cache/today_post_count.html';
    $cacheExpire = 3 * 60 * 60; // 3小时有效期

    if (file_exists($cacheFilePath) && time() - filemtime($cacheFilePath) < $cacheExpire) {
        return file_get_contents($cacheFilePath);
    } else {
        $todayPostCount = calculateTodayPostCount(); // 调用之前定义的获取当天文章数的函数

        // 将结果写入缓存文件
        file_put_contents($cacheFilePath, $todayPostCount);

        return $todayPostCount;
    }
}

function calculateTodayPostCount() {
    $today = date('Y-m-d');
    $nextDay = date('Y-m-d', strtotime('+1 day'));

    $query = Typecho_Db::get()->select()->from('table.contents')
              ->where('created >= ?', strtotime($today))
              ->where('created < ?', strtotime($nextDay))
              ->where('type = ?', 'post')
              ->where('status = ?', 'publish');

    $results = Typecho_Db::get()->fetchAll($query);
    $postsToday = count($results);

    return $postsToday;
}

记得在模板所在的文件夹里面建一个cache文件夹。缓存文件就存在这里了

然后在你要调用的地方加入 以下代码

<?php echo "今天更新的文章数:" . getTodayPostCount(); ?>

摘自:https://www.myhelen.cn/helen/226.htm

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:Typecho教程
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »