index.php

模板信息

我们先从主文件说起,打开这个文件,首先看到的是注释:

/**
 * 这是typecho系统的一套默认皮肤。你可以在<a href="http://typecho.org">typecho的官方网站</a>获得更多关于此皮肤的信息
 * 
 * @package Typecho Default Theme 
 * @author typecho
 * @version 1.0.0
 * @link http://typecho.org
 */

这是模板信息存放的地方,它将在后台的模板选择页显示。前两行是简短的介绍,每个“*”表示一个段落。

  • @package 表示模板名
  • @author表示作者名
  • @version是模板的版本号
  • @link是作者的网站连接

紧挨着注释下方的$this->need('header.php'),在结尾处也会看到$this->need('sidebar.php')$this->need('footer.php')。这些语句用来调用模板的其它模块。header故名思议是页首,sidebar是侧栏,footer是页脚。(与php的include功能差不多,need是typecho程序内置的方法,内部还会存在一些判断什么的,建议在做主题是使用need方法而不是include)

显示文章列表

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>

进入文章循环,输出文章,一句一句介绍

代码解释
<?php if ($this->have()): ?>判断是否有文章,没有的话输出提示
<?php while($this->next()): ?>开始循环输出文章,与<?php endwhile; ?>对应
<?php $this->permalink() ?>文章所在的连接
<?php $this->title() ?>文章标题
<?php $this->author(); ?>文章作者
<?php $this->author->permalink(); ?>文章作者地址
<?php $this->date(); ?>文章的发布日期,日期格式可在typecho后台设置->阅读中设置
<?php $this->category(','); ?>文章所在分类
<?php $this->tags(','); ?>文章标签
<?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>文章评论数及连接
<?php $this->content('- 阅读剩余部分 -'); ?>文章内容,其中的“- 阅读剩余部分 -”是显示摘要时隐藏部分的邀请链接,也可使用<?php $this->excerpt(140, '...'); ?>来进行自动截取文字内容,“140”是截取字符数量

文章分页

<?php $this->pageNav('«前一页', '后一页»'); ?>

也可以这样分开写

<?php $this->pageLink('下一页','next'); ?>
<?php $this->pageLink('上一页'); ?>

其他说明

archive.php代码同index.php,区别就是index.php是显示首页的,而archive.php是显示某分类下的文章列表、搜索结果的。如果模板文件中不存在archive.php,程序就会自动用index.php代替archive.php。

header.php

编码

打开这个文件,见到的第一个php代码就是:

<meta charset="<?php $this->options->charset(); ?>">

调用默认的编码,现在最经常用都是utf-8吧。所以我通常是直接写成utf-8,省去php处理时间。

页面标题

<?php $this->archiveTitle(array(
            'category'  =>  _t('分类 %s 下的文章'),
            'search'    =>  _t('包含关键字 %s 的文章'),
            'tag'       =>  _t('标签 %s 下的文章'),
            'author'    =>  _t('%s 发布的文章')
        ), '', ' - '); ?><?php $this->options->title(); ?>

<?php $this->archiveTitle(); ?>是当前页面的标题,<?php $this->options->title(); ?>是网站的标题。

导入样式

<link rel="stylesheet" href="<?php $this->options->themeUrl('style.css'); ?>">

其中style.css是样式表文件相对模板目录的路径和文件名。

其它HTML头部信息

<?php $this->header(); ?>

这是typecho的自有函数,会输出HTML头部信息;同时这个也是头部插件接口,有了它插件可以向网站头部插入css或者js代码。

网站名称与logo

<?php if ($this->options->logoUrl): ?>
<a id="logo" href="<?php $this->options->siteUrl(); ?>">
<img src="<?php $this->options->logoUrl() ?>" alt="<?php $this->options->title() ?>" />
</a>
<?php else: ?>
<a id="logo" href="<?php $this->options->siteUrl(); ?>"><?php $this->options->title() ?></a>
<p class="description"><?php $this->options->description() ?></p>
<?php endif; ?>

第一句的if判断是判断模板是否通过模板设置设置了logo的地址,如果设置了就显示logo图片,否则就显示博客标题。
<?php $this->options->siteUrl(); ?>是网站地址
<?php $this->options->title() ?>是网站名字
<?php $this->options->description() ?>是网站描述。
logo部分的讲解将会在functions.php章节中详细讲解。

站内搜索

<form id="search" method="post" action="<?php $this->options->siteUrl(); ?>" role="search">
<input type="text" id="s" name="s" class="text" placeholder="<?php _e('输入关键字搜索'); ?>" />
<button type="submit" class="submit"><?php _e('搜索'); ?></button>
</form>

当你的文章很多很多,这个搜索就必不可少。

页面导航

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
0

其中<?php $this->options->siteUrl(); ?>是网站地址,然后下面的while循环是循环输出独立页面的,其中<?php $pages->permalink(); ?>是独立页面的超链接,<?php $pages->title(); ?>是独立页面的标题。

sidebar.php

最新文章列表

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
1

获取最新的10篇文章标题,得到的html是

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
2

具体显示数量可在typecho后台设置->阅读中设置。

最新回复列表

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
3

获取最新的10个回复,得到的html是

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
4

其中<?php $comments->excerpt(35, '...'); ?>,“35”代表要回复内容截取的字的个数,“…”代表省略的意思,你可以自行修改。具体显示数量可在typecho后台设置->评论中设置。

文章分类列表

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
5

效果如下

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
6

如果有个分类3,分类4是上述分类2的子分类,那么效果如下

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
7

按月归档

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
8

输出:

<?php if ($this->have()): ?>
<?php while($this->next()): ?>
<a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
作者:<a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>
时间: <?php $this->date(); ?>
分类: <?php $this->category(','); ?>
<a href="<?php $this->permalink() ?>#comments"><?php $this->commentsNum('评论', '1 条评论', '%d 条评论'); ?>
标签:<?php $this->tags(','); ?>
<?php $this->content('- 阅读剩余部分 -'); ?>
<?php endwhile; ?>
<?php else: ?>暂无文章<?php endif; ?>
9

登陆登出

<?php $this->pageNav('«前一页', '后一页»'); ?>
0

这些是可有可无的,只是为了方便登录登出。<?php $this->options->adminUrl(); ?>是后台地址,<?php $this->user->screenName(); ?>用户昵称,<?php $this->options->logoutUrl(); ?>登出链接,<?php $this->options->adminUrl('login.php'); ?>登陆链接。

RSS地址

<?php $this->pageNav('«前一页', '后一页»'); ?>
1

footer.php

页脚文件,推荐大家把一些较大的js放在这个文件中最后载入,不会影响阅读。看看我们的footer要讲解些什么?

版权声明等

<?php $this->pageNav('«前一页', '后一页»'); ?>
2
  • <?php echo date('Y'); ?>是当前年份
  • <?php $this->options->siteUrl(); ?>是网站地址
  • <?php $this->options->title(); ?>是网站标题。

插件接口

<?php $this->pageNav('«前一页', '后一页»'); ?>
3

用于插件向页脚插入css,js文件等。

post.php

post页和index是差不多的,下面解释下post.php里面存在的php代码。

代码与说明

代码解释
<?php $this->permalink() ?>文章地址
<?php $this->title() ?>文章标题
<?php $this->author->permalink(); ?>文章作者主页链接
<?php $this->author(); ?>文章作者昵称
<?php $this->date(); ?>文章发布时间
<?php $this->category(','); ?>文章分类,多个分类中间用逗号隔开
<?php $this->content(); ?>文章内容
<?php $this->tags(', ', true, 'none'); ?>文章标签,多个标签间用逗号隔开,标签以带超链接的形式显示,如果不存在标签则显示none
<?php $this->need('comments.php'); ?>调用评论页
<?php $this->thePrev('%s','没有了'); ?>带有超链接的上一篇文章的标题
<?php $this->theNext('%s','没有了'); ?>带有超链接的下一篇文章的标题

其他说明

page.php代码同post.php,区别就是post是用来显示文章的,而page.php是用来显示独立页面的。

comments.php

评论列表

<?php $this->pageNav('«前一页', '后一页»'); ?>
4

判断文章是否存在评论,如果存在就输出评论;其中<?php $comments->listComments(); ?>是评论列表,<?php $comments->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>是评论翻页按钮。

评论输入表单

<?php $this->pageNav('«前一页', '后一页»'); ?>
5

具体请对应上述代码中注释自行理解。

functions.php

function themeConfig($form) 内的代码是模板设置功能

logo设置

<?php $this->pageNav('«前一页', '后一页»'); ?>
6

这行代码就是在模板设置处添加一个logo设置,可以添加一个图片地址作为logo,添加好了通过如下代码即可输出这个图片

<?php $this->pageNav('«前一页', '后一页»'); ?>
7

此处对应header.php中的logo显示。

显示开关

<?php $this->pageNav('«前一页', '后一页»'); ?>
8

是一些开关,这里拿ShowCategory举例,如果勾选他

<?php $this->pageNav('«前一页', '后一页»'); ?>
9

这里对应的是sidebar.php中的最新文章,最新评论,文章分类,归档等显示开关。

其他说明

参考以上代码,照葫芦画瓢,可以增加自己需要的模板设置。

文件结构说明

文件名作用必须
style.css主题样式文件
screenshot.png主题缩略图,图片后缀支持jpg,png,gif,bmp,jpeg
index.php首页以及说明文件
404.php404页面文件
archive.php通用(分类、搜索、标签、作者)页面文件
category.php分类页面文件
search.php搜索页面文件
tag.php标签页面文件
author.php作者页面文件
comments.php评论页面文件
footer.php底部页面文件
functions.php主题函数文件
header.php头部页面文件
page.php独立页面文件
post.php日志页面文件
sidebar.php侧边栏页面文件

PS:
如果archive.php不存在,index.php也会作为通用页面,实现archive.php的工作。