在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) ?>
转自 https://qqdie.com/archives/typecho-counts-the-plugin-implementation-article-reading.html
February 26th, 2020 at 10:05 pm