本文內容皆為作者原創,如需轉載,請注明出處:https://www.cnblogs.com/xuexianqi/p/13416445.html
一: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> >
<?php elseif ($this->is('post')): ?><!-- 頁面為文章單頁時 -->
<a href="<?php $this->options->siteUrl(); ?>" title="<?php $this->options->title(); ?>">首頁</a> > <?php $this->category(); ?> > <?php $this->title(); ?>
<?php else: ?><!-- 頁面為其他頁時 -->
<a href="<?php $this->options->siteUrl(); ?>" title="<?php $this->options->title(); ?>">首頁</a> > <?php $this->archiveTitle(' » ','',''); ?>
<?php endif; ?>
</div>