代码已中加入了cookie验证,让文章浏览次数更具有真实性
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'];
}
在需要显示次数的地方 (如 index.php,post.php) 加入下边的代码
阅读 <?php get_post_view($this) ?>
/* 自定义首页文章分布数量,如 10 */
function themeInit($archive) {
if ($archive->is('index')) {
$archive->parameter->pageSize = 10;
}
}
]]>header.php
<?php _e('首页'); ?></a>
在空行后面添加
<?php $this->widget('Widget_Metas_Category_List')->to($categories); ?>
<?php while($categories->next()): ?>
<a<?php if($this->is('category', $categories->slug)): ?> class="current"<?php endif; ?> href="<?php $categories->permalink(); ?>" title="<?php $categories->name(); ?>"><?php $categories->name(); ?></a>
<?php endwhile; ?>
]]><IfModule mod_rewrite.c>
RewriteEngine On
#如果开启了HTTPS服务,使用以下两行内容
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}/$1 [L,R=301]
#开启伪静态
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
]]><?php $this->content('- 阅读剩余部分 -'); ?>
替换成:
<?php $this->excerpt(300,'- 阅读剩余部分 -'); ?>
300这个数字,你怎么开心,怎么设置。
还有个好方法:
编辑文件
/var/Widget/Abstract/Contents.php
改成这样:
/**
* 输出文章摘要
*
* @access public
* @param integer $length 摘要截取长度
* @param string $trim 摘要后缀
*/
public function excerpt($length = 100, $trim = '...')
{
// Typecho_Common::subStr(strip_tags($this->excerpt), 0, $length, $trim)
echo Typecho_Common::subStr(strip_tags($this->excerpt), 0, $length, $trim) . "<p class=\"more\"><a href=\"{$this->permalink}\" title=\"{$this->title}\">- 阅读剩余部分 -</a></p>";
}
]]>