Typecho非插件实现文章阅读次数统计

在functions.php中加入下面代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//get_post_view($this)
function get_post_view($archive)
{
$cid = $archive->cid;
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {
$db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');
echo 0;
return;
}
$row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));
if ($archive->is('single')) {
$db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));
}
echo $row['views'];
}

在需要显示次数的地方(如index.phppost.php,page.php)加下边的代码

1
<?php get_post_view($this) ?>

来源:https://blog.zezeshe.com/


Typecho非插件实现文章阅读次数统计
http://yoursite.com/2022/08/18/typecho/get_post_view/
作者
雨停之後
发布于
2022年8月18日
许可协议