Typecho使用技巧


一:Typecho获取当前页面加载完成速度时间

判断当前页面加载是否快速,通常是直接在浏览器中访问网站,看自己的直观感受是否快速。而客观的方法则是计算具体的页面加载时间并显示出来给看。

 

1.在当前主题的functions.php文件添加下面的代码:

function timer_start() { global $timestart; $mtime = explode( ' ', microtime() ); $timestart = $mtime[1] + $mtime[0]; return true; } timer_start(); function timer_stop( $display = 0, $precision = 3 ) { global $timestart, $timeend; $mtime = explode( ' ', microtime() ); $timeend = $mtime[1] + $mtime[0]; $timetotal = number_format( $timeend - $timestart, $precision ); $r = $timetotal < 1 ? $timetotal * 1000 . " ms" : $timetotal . " s"; if ( $display ) { echo $r; } return $r; }
 

2.在要显示加载时间的位置添加调用代码

<?php echo timer_stop();?>
 

二:Typecho文章阅读次数统计功能

文章阅读次数是文章很重要的一个元素,通过它可以知道文章被访问的次数。博客吧前面介绍有Typecho浏览统计和热门文章调用插件TePostViews,通过该插件就可以实现统计每篇文章的阅读浏览次数。但是除了使用插件外,其实也可以直接在typecho主题中添加函数代码实现。此外下面分享的typecho阅读次数统计代码加入了cookie验证,重复刷新页面也只会增加一次阅读次数。

 

1.在当前主题的functions.php文件中添加以下代码:

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']; }
 

2.在主题的post.php(文章内容页面)index.php(列表页)page.php(单页面)文件中添加阅读次数调用代码:

<?php get_post_view($this) ?>
 

三:Typecho博客统计信息调用代码

以下代码实现的功能是在网站前端界面,调用整站的文章总数、分类总数、评论总数以及页面总数量显示在指定位置,操作简单,直接把代码复制使用即可,当然显示样式需要自行编写

 

1.统计代码:

<?php if ($this->options->sidebarBlock && in_array('showSiteStatistics', $this->options->sidebarBlock)): ?> <section class="widget"> <h3><?php _e('数据统计'); ?></h3> <ul> <?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?> <li><?php _e('文章数量:'); ?><?php $stat->publishedPostsNum() ?></li> <li><?php _e('分类数量:'); ?><?php $stat->categoriesNum() ?></li> <li><?php _e('评论数量:'); ?><?php $stat->publishedCommentsNum() ?></li> <li><?php _e('页面数量:'); ?><?php echo $stat->publishedPagesNum + $stat->publishedPostsNum; ?></li>
 

四:Typecho导航栏调用分类目录

Typecho的默认主题导航菜单部分调用的是独立页面,而我们搭建网站一般是把分类目录显示在导航栏,或者把分类目录和独立页面一起显示在导航栏,这样便于访客浏览网站目录

 

1.只显示分类目录

 

①在主题的header.php文件中找到代码:

<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?> <?php while($pages->next()): ?> <a<?php if($this->is('page', $pages->slug)): ?> class="current"<?php endif; ?> href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>"><?php $pages->title(); ?></a> <?php endwhile; ?>
 

②修改为以下代码:

<?php $this->widget('Widget_Metas_Category_List')->to($category); ?> <?php while($category->next()): ?> <li><a<?php if($this->is('category', $category->slug)): ?> class="current"<?php endif; ?> href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>"><?php $category->name(); ?></a></li> <?php endwhile; }?>
 

2.分类目录和独立页面都显示

 

①在主题的header.php文件中找到代码:

<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?> <?php while($pages->next()): ?> <a<?php if($this->is('page', $pages->slug)): ?> class="current"<?php endif; ?> href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>"><?php $pages->title(); ?></a> <?php endwhile; ?>
 

②在该代码上面添加代码

<?php $this->widget('Widget_Metas_Category_List')->to($category); ?> <?php while($category->next()): ?> <li><a<?php if($this->is('category', $category->slug)): ?> class="current"<?php endif; ?> href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>"><?php $category->name(); ?></a></li> <?php endwhile; }?>
 

五:Typecho自定义文章评论列表样式

Typecho文章评论列表通过$comments->listComments()函数代码即可调用,但存在一个问题,就是评论列表的HTML结构是固定的,限制了文章评论列表样式的设计空间,开发者只能根据其固定的HTML结构设计评论样式,庆幸的是typecho也支持自定义该函数的HTML代码

 

1.自定义单条评论的HTML代码

 

①在自定义评论前,先设计好单条评论的HTML代码结构,如:

<li id="li-comment-520" class="comment-body comment-parent comment-odd"> <div id="comment-520"> <div class="comment-author"> <img class="avatar" src="avatar.png" alt="" width="40" height="40"> <cite class="fn"><a href="评论者主页">评论者名字</a></cite> </div> <div class="comment-meta"> <a href="评论地址">评论时间</a> <span class="comment-reply">回复按钮</span> </div> <p>我是评论内容</p> </div><!-- 单条评论者信息及内容 --> <div class="comment-children"> <!-- 嵌套评论相关 --> </div> </li>
 

2.自定义评论函数

 

①编辑主题的comments.php文件,在文件中第一行添加以下函数代码

<?php function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; //如果是文章作者的评论添加 .comment-by-author 样式 } else { $commentClass .= ' comment-by-user'; //如果是评论作者的添加 .comment-by-user 样式 } } $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent'; //评论层数大于0为子级,否则是父级 ?> /* 自定义评论的代码结构 */ <?php } ?>
 

②把上面自定义单条评论的HTML代码放在自定义评论函数代码注释的地方,如下

<?php function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; //如果是文章作者的评论添加 .comment-by-author 样式 } else { $commentClass .= ' comment-by-user'; //如果是评论作者的添加 .comment-by-user 样式 } } $commentLevelClass = $comments->_levels > 0 ? ' comment-child' : ' comment-parent'; //评论层数大于0为子级,否则是父级 ?> /* 自定义评论的代码结构 */ <li id="li-comment-520" class="comment-body comment-parent comment-odd"> <div id="comment-520"> <div class="comment-author"> <img class="avatar" src="avatar.png" alt="" width="40" height="40"> <cite class="fn"><a href="评论者主页">评论者名字</a></cite> </div> <div class="comment-meta"> <a href="评论地址">评论时间</a> <span class="comment-reply">回复按钮</span> </div> <p>我是评论内容</p> </div><!-- 单条评论者信息及内容 --> <div class="comment-children"> <!-- 嵌套评论相关 --> </div> </li> <?php } ?>
 

3.用系统的评论变量替换HTML中相关属性

 

把HTML里相关的属性,替换成typecho系统中的评论变量,变量的列表可以参考下面。下面的例子,是替换评论的id,替换前

<li id="li-comment-520" class="comment-body comment-parent comment-odd">
 

替换后

<li id="li-<?php $comments->theId(); ?>" class="comment-body<?php if ($comments->_levels > 0) { echo ' comment-child'; $comments->levelsAlt(' comment-level-odd', ' comment-level-even'); } else { echo ' comment-parent'; } $comments->alt(' comment-odd', ' comment-even'); echo $commentClass; ?>">
 

注意:替换ID这里,还需要判断判断当前评论是父级评论还是子级评论,且判断评论 ID 的奇偶数等。

 

4.添加嵌套评论(子评论)

 

替换前:

 <div class="comment-children"> <!-- 嵌套评论相关 --> </div>`
 

替换后后如下:

<?php if ($comments->children) { ?> //是否嵌套评论判断开始 <div class="comment-children"> <?php $comments->threadedComments($options); ?> //嵌套评论所有内容 </div> <?php } ?> //是否嵌套评论判断结束
 

5.相关变量及说明

<?php $comments->gravatar('40', ''); ?> //头像,有两个参数,大小、默认头像? <?php $comments->author(); ?> //评论作者 <?php $comments->permalink(); ?> //当前评论的连接地址 <?php $comments->date('Y-m-d H:i'); ?>//评论时间,可在括号里设置格式 <?php $comments->reply(); ?> //回复按钮,可在括号里自定义评论按钮的文字 <?php $comments->content(); ?> //评论内容 <?php $singleCommentOptions->commentStatus(); ?> //首次评论审核提示
 

6.最终得到的代码

 

把上面所有变量都替换完成之后,最终得到的代码如下

<?php function threadedComments($comments, $options) { $commentClass = ''; if ($comments->authorId) { if ($comments->authorId == $comments->ownerId) { $commentClass .= ' comment-by-author'; } else { $commentClass .= ' comment-by-user'; } } $commentLevelClass = $comments->levels > 0 ? ' comment-child' : ' comment-parent'; ?> <li id="li-<?php $comments->theId(); ?>" class="comment-body<?php if ($comments->levels > 0) { echo ' comment-child'; $comments->levelsAlt(' comment-level-odd', ' comment-level-even'); } else { echo ' comment-parent'; } $comments->alt(' comment-odd', ' comment-even'); echo $commentClass; ?>"> <div id="<?php $comments->theId(); ?>"> <div class="comment-author"> <?php $comments->gravatar('40', ''); ?> <cite class="fn"><?php $comments->author(); ?></cite> </div> <div class="comment-meta"> <a href="<?php $comments->permalink(); ?>"><?php $comments->date('Y-m-d H:i'); ?></a> <span class="comment-reply"><?php $comments->reply(); ?></span> </div> <?php $comments->content(); ?> <?php $singleCommentOptions->commentStatus(); ?> </div> <?php if ($comments->children) { ?> <div class="comment-children"> <?php $comments->threadedComments($options); ?> </div> <?php } ?> </li> <?php } ?>
 

六:Typecho页面面包屑导航代码

在网站页面中常见的如“首页 » 新闻中心 » 正文”的东西就是面包屑,页面面包屑通常用来展示当前页面在网站中的位置,以及对网站的SEO优化起到促进作用,特别是网页结构层次较深的网站,因此面包屑几乎是网站必备的模块

 

面包屑代码:

<div class="breadcrumb"> <?php if($this->is('index')):?><!-- 页面首页时 --> <a href="<?php $this->options->siteUrl(); ?>" title="<?php $this->options->title(); ?>">首页</a> &gt; <?php elseif ($this->is('post')): ?><!-- 页面为文章单页时 --> <a href="<?php $this->options->siteUrl(); ?>" title="<?php $this->options->title(); ?>">首页</a> &gt; <?php $this->category(); ?> &gt; <?php $this->title(); ?> <?php else: ?><!-- 页面为其他页时 --> <a href="<?php $this->options->siteUrl(); ?>" title="<?php $this->options->title(); ?>">首页</a> &gt; <?php $this->archiveTitle(' &raquo; ','',''); ?> <?php endif; ?> </div>
 

把上面的代码添加到要显示面包屑位置对应的模板文件中,如header.php


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM