前段时间写Gztro for typecho版的时候涉及到了typecho的主题制作代码,基本上都是一些更换的,和WordPress有点差别,做个备忘,以便日后有需要的时候方便查看。
1.自定义一下标题,以下为代码和参考案例:
<?php if($this->_currentPage>1) echo '第 '.$this->_currentPage.' 页 – '; ?><?php $this->archiveTitle(", ", ' – '); ?><?php $this->options->title(); ?> <?php if($this->is('index')): ?> – 自定义关键词<?php endif; ?>
2.根据TAG调用相关文章:
<?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>
3.上一篇与下一篇调用代码:
<?php $this->thePrev(); ?> <?php $this->theNext(); ?>
4.全部文章列表,可应用于归档或网站地图,蜘蛛指引等:
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000′)->parse('<li>{year}-{month}-{day} : <a href="{permalink}">{title}</a></li>'); ?>
5.全部标签列表,按照MID排序:
<?php $this->widget('Widget_Metas_Tag_Cloud') ->to($taglist); ?><?php while($taglist->next()): ?> <li><a href="<?php $taglist->permalink(); ?>" title="<?php $taglist->name(); ?>"><?php $taglist->name(); ?></a></li> <?php endwhile; ?>
6.自定义标签数量(就这里面的20),按照文章数量排序:
<?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; ?>
7.自定义分类、标签、搜索、首页等文章分页数量,修改 functions.php 文件:
function themeInit($archive) { if ($archive->is('index')) { $archive->parameter->pageSize = 10; // 自定义条数 } }
或者:
function themeInit($archive) { if ($archive->is('category', 'default')) { $archive->parameter->pageSize = 10; // 自定义条数 } }
8.调用某分类文章,pageSize是数量,mid是分类号:
<?php $this->widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=47′) ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
9.判断为当前页的第几篇文章,并单独输出代码,可应用于第一篇文章底部广告:
<?php if ($this->sequence == 0): ?> //需要的插入 <?php endif; ?>
10.判断是否为首页,输出相关内容:
<?php if($this->is('index')): ?> //首页输出内容 <?php else: ?> //不是首页输出内容 <?php endif; ?>
11.判断当前分类,输出内容:
<?php if($this->category == “help"): ?> //当前分类为help缩略图,则输出内容。 <?php endif; ?>
12.首页不显示某分类内容:
<?php while($this->next()): ?> <?php if($this->category != “cateslug"): ?> //正常输出循环 <?php endif; ?> <?php endwhile; ?>
13.Typecho侧边栏不显示博主评论,sidebar.php文件:
将以下代码:
<?php $this->widget('Widget_Comments_Recent')->to($comments); ?>
修改为:
<?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>
14.例如24小时内发布的贴,需要一个标志来完成。这里是用判断输入特殊字符,再用CSS判断完成的。此代码由羽飞儿老师编写,案例可参考:
/** * 判断时间区间 * * 使用方法 if(timeZone($this->date->timeStamp)) echo 'ok'; */ function timeZone($from){ $now = new Typecho_Date(Typecho_Date::gmtTime()); return $now->timeStamp – $from < 24*60*60 ? true : false; }
以上代码,加入到 functions.php 中,然后,在 index.php 中使用如下调用:
<?php if(timeZone($this->date->timeStamp)) echo 'new'; ?>
注:这样就会输出一个new的文字,可应用于class里,然后,自定义输出背景图片等。
15.自定义首页描述description文字内容:
<?php if($this->is('index')): ?> <?php $this->header('description=详细描述内容部分内容,自定义即可。'); ?> <?php else: ?> <?php $this->header(); ?><?php endif; ?>
16.首页第一篇文章显示不一样:
<?php if (($this->_currentPage == 1) && ($this->sequence == 1)): ?> … //首页第一篇文章 <?php else: ?> … //其它文章 <?php endif; ?>
17.导航菜单不显示某分类或某页面:
<?php while ($pages->next()): ?> <?php while ($pages->next()): ?> //循环语句开始 <?php if ($pages->slug != 'about'): ?> // 记得闭合 if 语句 <?php if (($pages->slug != 'about') && ($pages->slug != 'links')): ?>
18.输出全部分类,并对当前分类current标记:
<ul id="nav_menu"> <?php $this->widget('Widget_Metas_Category_List')->to($category); ?> <?php while ($category->next()): ?> <li<?php if ($this->is('post')): ?><?php if ($this->category == $category->slug): ?> class="current"<?php endif; ?><?php else: ?><?php if ($this->is('category', $category->slug)): ?> class="current"<?php endif; ?><?php endif; ?>><a href="<?php $category->permalink(); ?>" title="<?php $category->name(); ?>"><?php $category->name(); ?></a></li> <?php endwhile; ?> </ul>
19.像CMS那样,输出全部分类,并按分类输出文章:
/* 循环所有的分类 */ <?php $this->widget('Widget_Metas_Category_List')->to($categories); ?> <?php while ($categories->next()): ?> /* 循环当前分类下的文章 */ <?php $this->widget('Widget_Archive@category-' . $categories->mid, 'pageSize=7&type=category', 'mid=' . $categories->mid)->to($posts); ?> <div class="posts"> <?php while ($posts->next()): ?> <?php if (1 == $posts->sequence): ?> //判断第一篇文章 <h3>[<?php $categories->name(); ?>]:<a href="<?php $posts->permalink(); ?>"><?php $posts->title(43); ?></a></h3> <?php $posts->excerpt(120, '…'); ?> <ul class="post-list"> //文章列表 <?php else: ?> <li> <a href="<?php $posts->permalink(); ?>"><?php $posts->title(40); ?></a> <span class="comment-num">(<?php $posts->commentsNum(); ?>)</span> </li> <?php endif; ?> <?php endwhile; ?> </ul> </div> <?php endwhile; ?>
20.前台输出相关统计:
<?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?> <p><?php _e('不烦恼的博客自 <strong>2011</strong> 年初建立以来,截至 %s 在已设定的 <strong>%s</strong> 个分类 和 <strong>%s</strong> 个页面中, 共发布了 <strong>%s</strong> 篇文章,并收到了 <strong>%s</strong> 条相关评论。 ', date('Y年n月j日G时i分'), $stat->categoriesNum, $stat->publishedPagesNum, $stat->publishedPostsNum, $stat->publishedCommentsNum); ?></p>
21.调用单独页面评论代码,存在一个php,单独引用:
<?php /** * 单独页面调用评论列表 * * @author Mr.Asong * @link http://mrasong.com */ $slug = “message"; //页面缩略名 $limit = 10; //调用数量 $length = 30; //截取长度 $ispage = true; //true 输出slug页面评论,false输出其它所有评论 $isGuestbook = $ispage ? " = " : " <> “; $db = $this->db; //Typecho_Db::get(); $options = $this->options; //Typecho_Widget::widget('Widget_Options'); $page = $db->fetchRow($db->select()->from('table.contents') ->where('table.contents.status = ?', 'publish') ->where('table.contents.created < ?', $options->gmtTime) ->where('table.contents.slug = ?', $slug)); if ($page) { $type = $page['type']; $routeExists = (NULL != Typecho_Router::get($type)); $page['pathinfo'] = $routeExists ? Typecho_Router::url($type, $page) : '#'; $page['permalink'] = Typecho_Common::url($page['pathinfo'], $options->index); $comments = $db->fetchAll($db->select()->from('table.comments') ->where('table.comments.status = ?', 'approved') ->where('table.comments.created < ?', $options->gmtTime) ->where('table.comments.type = ?', 'comment') ->where('table.comments.cid ' . $isGuestbook . ' ?', $page['cid']) ->order('table.comments.created', Typecho_Db::SORT_DESC) ->limit($limit)); foreach ($comments AS $comment) { echo '<li>'; echo '<a href="' . $page['permalink'] . “#comment-" . $comment['coid'] . '" title="' . $comment['text'] . '">'; echo Typecho_Common::subStr(strip_tags($comment['text']), 0, $length, '…') . '</a>'; echo '</li>'; } } else { echo “<li>No Comments</li>"; } //不需要结束标志,并空一行 22.自定义pagenv分页盒样式: <?php $this->pageNav('上一页文字', '下一页文字', '默认显示数目', '省略符号'); ?>
23.创建自定义首页或页面模板:
<?php /** * 自定义首页模板 * * @package index */ <?php /** * 自定义页面模板 * * @package custom */
24.自定义调用某分类,并输出缩略图:
<?php $this->widget('Widget_Archive@index', 'pageSize=4&type=category', 'mid=23′)->to($indexpub); ?> <?php while($indexpub->next()): ?> <?php $indexpub->permalink(); ?> <?php $indexpub->title() ?> <?php Fimg_Plugin::showfimg($indexpub->cid,4);?> <?php $indexpub->excerpt(80, '……'); ?> <?php endwhile; ?>
25.自定义首页keywords和Description内容:
使用的是自定义的page模板做为首页,页page页面不输出关键词和描述。于是查阅了一下官方文档,得出以下结论,自定义这部分内容:
打开 header.php 文件:
<?php $this->header(); ?>
修改为:
<?php if($this->is('index')): ?> <?php $this->header('description=此处输入你的关键词'); ?> <?php else: ?> <?php $this->header(); ?><?php endif; ?>
26,公告轮播
模板header.php或者你想显示的地方
<?php $tip = $this->options->ac; //后台对应的FORMNAME $tip = str_replace("\r","",$tip); $tips = explode("\n",$tip); if(is_array($tips)){ foreach($tips as $tip){ $str .= '<li>'.$tip.'</li>'."\n\r"; } echo $str; //也可以return 看前台如何调 } ?>
在functions.php的themeConfig($form)里加
$ac = new Typecho_Widget_Helper_Form_Element_Textarea('ac', NULL, NULL, _t('站点公告'), _t('每条一行,显示在导航条右上方,支持HTML代码。(建议不要再这里插入一些影响布局的代码)')); $form->addInput($ac);
27,获取分类描述
<?php echo $this->getDescription(); ?>
后续继续补充。。。
好多啊
@W3Cshare 整理下,以后会用到
你好,第19中怎样输出文章缩略图?
谢谢博主,辛苦了
12.首页不显示某分类内容。会导致首页的文章越来越少。用上插件,就连feed都不输出(我想只是首页屏蔽,feed可以输出)
@灰狼 这代码都是两年前的了,你竟然看到这里了。
收藏了