首页 小组 问答 话题 好文 素材 用户 唠叨 我的社区

[分享]【Typecho技巧 】代码实现文章阅读次数统计

天启Lv.1普通用户
2024-08-18 17:56:13
0
57


将下面这段代码加到functions.php中


/**
* 阅读统计
* 调用
*/
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'];
}

然后将下面这段代码加入到需要显示阅读次数的地方

<?php get_post_view($this) ?>

这样就实现了不用插件来显示文章的阅读次数统计。

天启
天启

65 天前

签名 : 大运河向南是我家   57       0
评论
站长交流