Typecho非插件实现文章阅读次数统计
时间:4年前 阅读:1807
在functions.php中加入下面代码
//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.php,post.php,page.php)加下边的代码
<?php get_post_view($this) ?>
这个方法使用的数据和Hanny的Stat插件使用是同一个,可以方便替换掉Star插件,当然你也可以自行修改代码中所引用的数据列。
文章中代码转自:空樱酱的typecho主题制作总结
网友评论