文章阅读次数是文章很主要的一个元素,经由过程它能够晓得文章被接见的次数。博客吧前面引见有Typecho阅读统计和热点文章挪用插件TePostViews,经由过程该插件就能够完成统计每篇文章的阅读阅读次数。然则除了运用插件外,实在也能够直接在typecho主题中增添函数代码完成。另外下面分享的typecho阅读次数统计代码加入了cookie考证,反复革新页面也只会增添一次阅读次数。
操纵步骤:
在当前运用的typecho主题的functions.php文件中增添以下代码:
1234567891011121314151617181920212223242526 | 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')) { $views = Typecho_Cookie::get('extend_contents_views');if(empty($views)){$views = array();}else{$views = explode(',', $views);}if(!in_array($cid,$views)){ $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));array_push($views, $cid);$views = implode(',', $views);Typecho_Cookie::set('extend_contents_views', $views); //纪录检察cookie}}echo $row['views'];} |
在主题的post.php(文章内容页面)、index.php(列表页)或page.php(单页面)文件中增添阅读次数挪用代码:
1 | <?php get_post_view($this) ?> |
结果能够看博客吧的Typecho Seven主题:https://www.boke8.net/teseven.html
代码来自https://qqdie.com/archives/typecho-read-statistics.html
文章由 博客吧 整顿宣布

评论列表