本文将收集并记录在 Typecho 主题开发中一些比较常用的代码以备不时之需,当然这并不仅仅只是为了开发自己的主题而做准备,个人站长对站点进行 SEO 优化也会涉及到。 其实 typecho 不太符合 SEO 的标准,个人建站还是首推 Zblog,如果你跟我一样对 typecho 的喜欢有些偏执,同时又注重 SEO 的朋友,不妨先准备起来。
输出文章缩略图 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function showThumbnail ($widget ) { $rand = rand(1 ,5 ); $random = $widget ->widget('Widget_Options' )->themeUrl . '/img/sj/' . $rand . '.jpg' ; $attach = $widget ->attachments(1 )->attachment; $pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i' ;if (preg_match_all($pattern , $widget ->content, $thumbUrl )) { echo $thumbUrl [1 ][0 ]; } else if ($attach ->isImage) { echo $attach ->url; } else { echo $random ; } }
调用方法 1 <?php showThumbnail($this ); ?>
获取文章第一张图片做缩略图 将下面的代码添加到 functions.php 中
1 2 3 4 5 6 7 8 9 10 11 function showThumbnail ($widget ) {$attach = $widget ->attachments(1 )->attachment;$pattern = '/\<img.*?src\=\"(.*?)\"[^>]*>/i' ;if (preg_match_all($pattern , $widget ->content, $thumbUrl )) {echo $thumbUrl [1 ][0 ]; } else if ($attach ->isImage) {echo $attach ->url; } else {echo $random ; } }
调用代码 1 <img src =" <?php showThumbnail($this ); ?> " >
评论自动排第一 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function Autofirst ( ) { $db = Typecho_Db::get(); $query = $db ->select()->from('table.comments' )->where('authorId = ?' ,'0' )->order('coid' ,Typecho_Db::SORT_DESC)->limit(100 ); $result = $db ->fetchAll($query ); $arrUrl = array (); $arrAuthor = array (); foreach ($result as $value ) { if ($value ["url" ]!==null ){ array_push($arrUrl ,$value ["url" ]); array_push($arrAuthor ,$value ["author" ]); } } $su =array_filter(array_merge(array_unique($arrUrl ))); $sa =array_filter(array_merge(array_unique($arrAuthor ))); $num =0 ; for ($i =0 ;$i <count(array_unique($su ));$i ++){ if ($su [$i ]!=="" && $num <8 ){ $num +=1 ; $db1 = Typecho_Db::get(); $query1 = $db1 ->select()->from('table.comments' )->where('url = ?' ,$su [$i ])->order('coid' ,Typecho_Db::SORT_DESC)->limit(100 ); $result1 = $db1 ->fetchAll($query1 ); $arrAuthor1 = array (); foreach ($result1 as $value ) { array_push($arrAuthor1 ,$value ["author" ]); } echo '<div class="col-lg-3 col-md-3 item"><a href="' .$su [$i ].'" rel="external nofollow" class="btn btn-default btn-block overflow" target="_blank">' .$arrAuthor1 [0 ].'</a></div>' ; } } }
调用方法:
Title增加副标题: 1 <?php if ($this ->is('index' )): ?> - 副标题<?php endif ; ?>
分页标题 可以用下面的代码替换 header.php 文件中的中间部分的代码
1 <title><?php if ($this ->_currentPage>1 ) echo '第 ' .$this ->_currentPage.' 页 - ' ; ?> <?php $this ->archiveTitle(' » ' , '' , ' - ' ); ?> <?php $this ->options->title(); ?> </title>
typecho按分类输出cms: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <div class="row"><?php $this ->widget('Widget_Metas_Category_List' )->to($categories ); ?> <?php while ($categories ->next()): ?> <?php if (count($categories ->children) === 0 ): ?> <?php $this ->widget('Widget_Archive@category-' . $categories ->mid, 'pageSize=5&type=category' , 'mid=' . $categories ->mid)->to($posts ); ?> <div class="col-lg-6"> <div class="panel panel-default"> <div class="panel-heading"> <i class="glyphicon glyphicon-th"></i> <a href="<?php $categories->permalink(); ?>" class="guidang" id="posts-list-<?php $categories->slug(); ?>"><?php $categories->name(); ?></a> <a href="<?php $categories->permalink(); ?>" class="pull-right" id="posts-list-<?php $categories->slug(); ?>"><i class="glyphicon glyphicon-option-horizontal"></i></a> </div> <div class="panel-body"> <?php while ($posts ->next()): ?> <p class="overflow"> <a href="<?php $posts->permalink(); ?>" title="<?php $posts->title(40); ?>"><i class="glyphicon glyphicon-chevron-right"></i> <?php $posts->title(40); ?></a> </p> <?php endwhile ; ?> </div> </div> </div><?php else : ?> <?php endif ; ?> <?php endwhile ; ?> </div>
typecho随机文章 1、把下面的代码添加至主题的 functions.php 文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 function getRandomPosts ($limit = 10 ) { $db = Typecho_Db::get(); $result = $db ->fetchAll($db ->select()->from('table.contents' ) ->where('status = ?' ,'publish' ) ->where('type = ?' , 'post' ) ->where('created <= unix_timestamp(now())' , 'post' ) ->limit($limit ) ->order('RAND()' ) ); if ($result ){ $i =1 ; foreach ($result as $val ){ if ($i <=3 ){ $var = ' class="red"' ; }else { $var = '' ; } $val = Typecho_Widget::widget('Widget_Abstract_Contents' )->push($val ); $post_title = htmlspecialchars($val ['title' ]); $permalink = $val ['permalink' ]; echo '<li><i' .$var .'>' .$i .'</i><a href="' .$permalink .'" title="' .$post_title .'" target="_blank">' .$post_title .'</a></li>' ; $i ++; } } }
2、调用代码 1 <?php getRandomPosts('10' );?>
typecho相关文章 1 2 3 4 5 6 <?php $this ->related(5 )->to($relatedPosts ); ?> <ul> <?php while ($relatedPosts ->next()): ?> <li><a href="<?php $relatedPosts ->permalink(); ?>" title="<?php $relatedPosts ->title(); ?>" ><?php $relatedPosts ->title(); ?> </a></li> <?php endwhile ; ?> </ul>
typecho热门文章 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 function getHotComments ($limit = 10 ) { $db = Typecho_Db::get(); $result = $db ->fetchAll($db ->select()->from('table.contents' ) ->where('status = ?' ,'publish' ) ->where('type = ?' , 'post' ) ->where('created <= unix_timestamp(now())' , 'post' ) ->limit($limit ) ->order('commentsNum' , Typecho_Db::SORT_DESC) ); if ($result ){ foreach ($result as $val ){ $val = Typecho_Widget::widget('Widget_Abstract_Contents' )->push($val ); $post_title = htmlspecialchars($val ['title' ]); $permalink = $val ['permalink' ]; echo '<li><a href="' .$permalink .'" title="' .$post_title .'" target="_blank">' .$post_title .'</a></li>' ; } } } ```php<?php getHotComments('10' );?>
typecho侧边栏热门标签 1 2 3 4 5 6 7 8 9 10 <div class="widget"> <h3><?php _e('热门标签' ); ?> </h3> <ul class="cate"><?php $this ->widget('Widget_Metas_Tag_Cloud' , array ('sort' => 'count' , 'ignoreZeroCount' => true , 'desc' => true , 'limit' => 20 ))->to($tags ); ?> <?php while ($tags ->next()): ?> <li><a rel="tag" href="<?php $tags ->permalink(); ?>" ><?php $tags ->name(); ?> </a></li><?php endwhile ; ?> <div class="clear"></div> </ul> </div>
以上调用不需要插件支持的,完全 typecho 自带的,大致意思如下:
'sort' => 'count' 应该是表示按标签数量排序;
'ignoreZeroCount' => true 应该是表示过滤掉数量为0的空标签;
'limit' => 20 应该是表示调用标签数量为20个 标签的样式,由 CSS 控制,自行修改。
侧边栏输出所有标签 1 2 3 4 5 6 7 8 9 10 <div class="widget"> <h3><?php _e('所有标签' ); ?> </h3> <ul class="cate"><?php $this ->widget('Widget_Metas_Tag_Cloud' )->to($tags ); ?> <?php while ($tags ->next()): ?> <li><a rel="tag" href="<?php $tags ->permalink(); ?>" ><?php $tags ->name(); ?> </a></li><?php endwhile ; ?> <div class="clear"></div> </ul> </div>
文章阅读量统计 在 functions.php 中加入下面代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 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) ?> 这个方法使用的数据和 Hanny 的 Stat 插件使用是同一个,可以方便替换掉 Star 插件,当然你也可以自行修改代码中所引用的数据列。
** 来源 ** :https://www.moewah.com/archives/63.html