— typecho — 1 min read
在博客迁移到Typecho文章中,介绍了迁移到Typecho
做的一些配置。配置后也能符合我的需求了。为了让默认主题发挥出更好的效果,在原基础上对其进一步优化。主要优化的内容有:
如果你不想动手配置,可以在文末下载我修改后的主题。直接下载到主题目录即可切换使用了。
下面就依次对以上优化内容进行配置:
Typecho
默认主题的上下篇文章是在评论输入框后面的。如果前置上评论前会和文章之间的关联性更强,方便用户点击查看。
编辑post.php
文件,默认的代码顺序如下:
1<?php $this->need('comments.php'); ?>23 <ul class="post-near">4 <li>上一篇: <?php $this->thePrev('%s','没有了'); ?></li>5 <li>下一篇: <?php $this->theNext('%s','没有了'); ?></li>6 </ul>
将行尾部分的上下篇代码前置到评论前:
1<ul class="post-near">2 <li>上一篇: <?php $this->thePrev('%s','没有了'); ?></li>3 <li>下一篇: <?php $this->theNext('%s','没有了'); ?></li>4 </ul>56 <?php $this->need('comments.php'); ?>
保存退出刷新页面,即可查看到效果。
Typecho
的文章页面默认没有提供编辑的功能,需要登入到后台管理文章时才可以编辑。像WordPress
之类的博客平台都有提供文章页面编辑功能。这个功能可以让有权限的用户快速的编辑文章。
在默认主题文件夹下编辑post.php
文件,在带post-meta
属性的ul
标签下增加以下代码:
1<?php if($this->user->hasLogin()):?>2<a href="<?php $this->options->adminUrl(); ?>write-post.php?cid=<?php echo $this->cid;?>">编辑</a>3<?php endif;?>
同文章页面设置,在默认主题文件夹下编辑’page.php文件,在带'post-content'属性的
div`标签之前增加以下代码:
1<?php if($this->user->hasLogin()):?>2<a href="<?php $this->options->adminUrl(); ?>write-page.php?cid=<?php echo $this->cid;?>" >编辑</a>3<?php endif;?>
在默认主题文件夹下编辑'functions.php`,在行尾增加以下代码:
1/**2* 阅读次数统计3*4*/5function get_post_view($archive)6{7 $cid = $archive->cid;8 $db = Typecho_Db::get();9 $prefix = $db->getPrefix();10 if (!array_key_exists('views', $db->fetchRow($db->select()->from('table.contents')))) {11 $db->query('ALTER TABLE `' . $prefix . 'contents` ADD `views` INT(10) DEFAULT 0;');12 echo 0;13 return;14 }15 $row = $db->fetchRow($db->select('views')->from('table.contents')->where('cid = ?', $cid));16 if ($archive->is('single')) {17 $views = Typecho_Cookie::get('extend_contents_views');18 if(empty($views)){19 $views = array();20 }else{21 $views = explode(',', $views);22 }23if(!in_array($cid,$views)){24 $db->query($db->update('table.contents')->rows(array('views' => (int) $row['views'] + 1))->where('cid = ?', $cid));25array_push($views, $cid);26 $views = implode(',', $views);27 Typecho_Cookie::set('extend_contents_views', $views); //记录查看cookie28 }29 }30 echo $row['views'];31}
在需要统计阅读量的页面,如文章查看页增加阅读量。可以在默认主题文件夹下编辑post.php
文件,在带post-meta
属性的ul
标签下增加以下代码:
1<li><?php _e('阅读量: '); ?><?php get_post_view($this); ?></li>
同阅读次数统计,先在默认主题文件夹下编辑functions.php
,在行尾添加以下内容:
1/**2 * 加载时间3 * @return bool4 */5 function timer_start() {6 global $timestart;7 $mtime = explode( ' ', microtime() );8 $timestart = $mtime[1] + $mtime[0];9 return true;10 }11 timer_start();12 function timer_stop( $display = 0, $precision = 3 ) {13 global $timestart, $timeend;14 $mtime = explode( ' ', microtime() );15 $timeend = $mtime[1] + $mtime[0];16 $timetotal = number_format( $timeend - $timestart, $precision );17 $r = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s";18 if ( $display ) {19 echo $r;20 }21 return $r;22 }
然后在需要统计阅读次数的页面,比如文章查看页增加加载时间统计。可以在默认主题文件夹下编辑post.php
文件,在带post-meta
属性的ul
标签下增加以下代码:
1<li><?php _e('加载时间: '); ?><?php echo timer_stop();?> </li>
在做好以上的配置后,我们就可以刷新页面查看效果了。在配置后备份自己的主题,方便以后重装使用。
附上我修改后的主题Typecho-leisre,我给其取名为leisre
,即Less is more
的缩写。
(完)