Typecho免插件纯代码实现文章字数统计功能
一、前言
许多站点的文章页都有字数统计功能,显得高大上,其实typecho也是有相关插件的,但是秉着能用代码解决绝不用插件的原则,就搜了纯代码实现该功能的教程,类似文章也很多,转载过来以备使用。
二、具体步骤
在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) ?>
评论列表加@
function get_comment_at($coid)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('parent')->from('table.comments')
->where('coid = ? AND status = ?', $coid, 'approved'));
$parent = $prow['parent'];
if ($parent != "0") {
$arow = $db->fetchRow($db->select('author')->from('table.comments')
->where('coid = ? AND status = ?', $parent, 'approved'));
$author = $arow['author'];
$href = '<a href="#comment-' . $parent . '">@' . $author . '</a>';
echo $href;
} else {
echo '';
}
}
参考文章:
全部评论 (暂无评论)
info 还没有任何评论,你来说两句呐!