Typecho主题完整API文档
本文档基于Typecho默认模板代码和官方文档整理,涵盖了所有可用的PHP模板API。
整理时间:2026年2月
模板路径:
/home/nmb/code/博客UI增强/md/default/
📋 目录结构
default/
├── 404.php # 404错误页面
├── comments.php # 评论模块
├── footer.php # 页脚模块
├── functions.php # 主题配置与自定义函数
├── header.php # 页头模块
├── index.php # 首页/文章列表
├── page.php # 独立页面
├── post.php # 文章详情页
├── sidebar.php # 侧边栏
├── style.css # 主题样式
├── grid.css # 网格布局样式
├── normalize.css # CSS重置样式
├── screenshot.png # 主题缩略图
└── img/ # 图片资源目录📊 API统计概览
| 类别 | 数量 | 说明 |
|---|---|---|
核心对象 $this API |
约35个 | 模板核心方法 |
选项对象 $this->options API |
约18个 | 站点设置方法 |
用户对象 $this->user API |
3个 | 用户相关方法 |
| 评论对象 API | 约15个 | 评论相关方法 |
| Widget类 API | 8个 | 内容组件类 |
| 全局函数 API | 6个 | 辅助函数 |
| 表单元素 API | 3种 | 主题配置元素 |
文件结构说明
| 文件名 | 作用 | 必须性 |
|---|---|---|
| style.css | 主题样式文件 | 否 |
| screenshot.png | 主题缩略图 | 否 |
| index.php | 首页以及说明文件 | 是 |
| 404.php | 404页面文件 | 否 |
| archive.php | 通用(分类、搜索、标签、作者)页面文件 | 否 |
| category.php | 分类页面文件 | 否 |
| search.php | 搜索页面文件 | 否 |
| tag.php | 标签页面文件 | 否 |
| author.php | 作者页面文件 | 否 |
| comments.php | 评论页面文件 | 否 |
| footer.php | 底部页面文件 | 否 |
| functions.php | 主题函数文件 | 否 |
| header.php | 头部页面文件 | 否 |
| page.php | 独立页面文件 | 否 |
| post.php | 日志页面文件 | 否 |
| sidebar.php | 侧边栏页面文件 | 否 |
注:如果archive.php不存在,index.php也会作为通用页面,实现archive.php的工作。
一、核心对象 $this API
1.1 模板加载API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
need() |
\(file` | void | 引入模板文件 | `\)this->need('header.php')$this->need('footer.php') |
1.2 页面类型判断API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
is() |
\(type, \)param |
bool | 判断当前页面类型 | \(this->is('index')`<br>`\)this->is('post')\(this->is('page')`<br>`\)this->is('category')\(this->is('category', 'default')`<br>`\)this->is('tag')\(this->is('author')`<br>`\)this->is('archive')\(this->is('single')`<br>`\)this->is('search') |
页面类型判断示例:
<?php if ($this->is('post')): ?>
<!-- 文章单页内容 -->
<?php elseif ($this->is('page')): ?>
<!-- 独立页面内容 -->
<?php elseif ($this->is('category', 'default')): ?>
<!-- 默认分类内容 -->
<?php endif; ?>1.3 文章循环API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
have() |
无 | bool | 是否有文章 | $this->have() |
next() |
无 | void | 移动到下一篇文章 | $this->next() |
文章循环示例:
<?php if ($this->have()): ?>
<?php while ($this->next()): ?>
<!-- 文章内容 -->
<h2><?php $this->title() ?></h2>
<div><?php $this->content() ?></div>
<?php endwhile; ?>
<?php else: ?>
<h2>没有找到内容</h2>
<?php endif; ?>1.4 文章内容API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
content() |
\(more` | void | 输出文章内容 | `\)this->content(_t('阅读剩余部分'))$this->content('Continue Reading...') |
|||
title() |
无 | void | 输出文章标题 | \(this->title()`<br>`\)archive->title() |
permalink() |
无 | void | 输出文章链接 | \(this->permalink()`<br>`\)archive->permalink() |
date() |
\(format` | void | 输出日期 | `\)this->date('F j, Y')\(this->date('c')`<br>`\)archive->date() |
日期格式参考:
Y- 4位年份 (如 2026)m- 月份 (01-12)d- 日期 (01-31)H:i- 时间 (24小时制)F j, Y- 英文日期格式 (如 February 3, 2026)
1.5 标签API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
tags() |
\(split, \)link, \(default` | void | 输出标签 | `\)this->tags(', ', true, 'none') |
参数说明:
$split: 标签分隔符$link: 是否显示为链接$default: 没有标签时显示的内容
1.6 分类API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
category() |
\(split` | void | 输出分类 | `\)this->category(',')$archive->category(',') |
1.7 作者API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
author() |
无 | Object | 获取作者对象 | \(this->author()`<br>`\)archive->author() |
作者对象方法:
$archive->author(); // 输出作者名
$archive->author->permalink(); // 输出作者链接1.8 评论API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
comments() |
\(type` | Object | 获取评论对象 | `\)this->comments()->to(\(comments)`<br>`\)this->comments('comment')->to(\(comments)`<br>`\)this->comments('trackback')->to(\(trackbacks)`<br>`\)this->comments('pingback')->to($pingbacks) |
|||
commentsNum() |
\(none, \)one, \(more` | void | 输出评论数 | `\)this->commentsNum(_t('暂无评论'), _t('1 条评论'), _t('%d 条评论')) |
|||
allow() |
\(capability` | bool | 判断权限 | `\)this->allow('comment') |
|||
commentUrl() |
无 | void | 输出评论提交URL | $this->commentUrl() |
respondId() |
无 | void | 输出评论回复区ID | $this->respondId() |
remember() |
\(field` | void | 输出记住的字段 | `\)this->remember('author')\(this->remember('mail')`<br>`\)this->remember('url')$this->remember('text') |
评论类型过滤:
// 显示全部(默认)
\(this->comments()->to(\)comments);
// 只显示评论
\(this->comments('comment')->to(\)comments);
// 只显示引用通告
\(this->comments('trackback')->to(\)trackbacks);
// 只显示引用通知
\(this->comments('pingback')->to(\)pingbacks);1.9 分页API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
pageNav() |
\(prev, \)next, \(splitPage, \)prevWord, \(nextWord` | void | 输出分页导航 | `\)this->pageNav()$this->pageNav('« 前一页', '后一页 »') |
1.10 上下文导航API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
thePrev() |
\(format, \)default |
void | 输出上一篇链接 | $this->thePrev('%s', _t('没有了')) |
theNext() |
\(format, \)default |
void | 输出下一篇链接 | $this->theNext('%s', _t('没有了')) |
1.11 归档标题API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
archiveTitle() |
\(format, \)before, \(end` | void | 输出归档标题 | `\)this->archiveTitle(['category' => _t('分类 %s 下的文章'), 'search' => _t('包含关键字 %s 的文章'), 'tag' => _t('标签 %s 下的文章'), 'author' => _t('%s 发布的文章')], '', ' - ') |
参数说明:
$format: 多级菜单间的分隔符,如»或-$before: title 前显示的字符$end: title 后显示的字符
自定义title示例:
// 默认:站点名称 » 页面标题
<?php \(this->options->title(); ?><?php \)this->archiveTitle(); ?>
// 自定义:页面标题 - 站点名称
<?php \(this->archiveTitle('', '', ' - '); ?><?php \)this->options->title(); ?>1.12 头部/底部输出API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
header() |
\(keywords` | void | 输出HTML头部信息 | `\)this->header()$this->header('keywords=&generator=&template=&pingback=&xmlrpc=&wlw=') |
|||
footer() |
无 | void | 输出底部脚本 | $this->footer() |
头部信息过滤参数:
keywords: 关键词description: 描述、摘要rss1: feed rss1.0rss2: feed rss2.0atom: feed atomgenerator: 程序版本等template: 模板名称pingback: 文章引用xmlrpc: 离线写作wlw: Windows Live Writer离线写作commentReply: 评论回复
1.13 相关文章API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
related() |
\(limits, \)type |
Object | 获取相关文章 | \(this->related(5)->to(\)relatedPosts)\(this->related(5, 'author')->to(\)relatedPosts) |
参数说明:
$limits: 默认值为5,表示显示的相关文章数量\(type`: 默认值为NULL,表示文章的相关方式,只接受`author`。当`\)type为author时,根据用户显示相关文章;为其他值时,根据标签显示相关文章
相关文章调用示例:
<?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>二、选项对象 $this->options API
2.1 基础设置API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
title() |
无 | void | 输出站点标题 | $this->options->title() |
description() |
无 | void | 输出站点描述 | $this->options->description() |
charset() |
无 | void | 输出字符编码 | $this->options->charset() |
siteUrl() |
无 | void | 输出站点URL | $this->options->siteUrl() |
2.2 主题设置API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
themeUrl() |
\(file` | void | 输出主题文件URL | `\)this->options->themeUrl('style.css')\(this->options->themeUrl('normalize.css')`<br>`\)this->options->themeUrl('grid.css') |
|||
logoUrl |
无 | string | 自定义Logo地址 | $this->options->logoUrl |
sidebarBlock |
无 | array | 侧边栏配置 | $this->options->sidebarBlock |
2.3 评论设置API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
commentsRequireMail |
无 | bool | 评论是否必填邮箱 | $this->options->commentsRequireMail |
commentsRequireUrl |
无 | bool | 评论是否必填网址 | $this->options->commentsRequireUrl |
2.4 用户相关API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
profileUrl() |
无 | void | 输出个人资料URL | $this->options->profileUrl() |
logoutUrl() |
无 | void | 输出登出URL | $this->options->logoutUrl() |
adminUrl() |
\(url` | void | 输出后台管理URL | `\)this->options->adminUrl()$this->options->adminUrl('login.php') |
|||
index() |
\(path` | void | 输出特定功能URL | `\)this->options->index('Logout.do') |
2.5 RSS订阅API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
feedUrl() |
无 | void | 输出文章RSS | $this->options->feedUrl() |
commentsFeedUrl() |
无 | void | 输出评论RSS | $this->options->commentsFeedUrl() |
三、用户对象 $this->user API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
hasLogin() |
无 | bool | 用户是否已登录 | $this->user->hasLogin() |
screenName() |
无 | void | 输出用户昵称 | $this->user->screenName() |
四、评论对象 API ($comments)
4.1 评论循环API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
to() |
\(object` | void | 将评论赋值给变量 | `\)this->comments()->to($comments) |
|||
have() |
无 | bool | 是否有评论 | $comments->have() |
next() |
无 | void | 移动到下一条评论 | $comments->next() |
4.2 评论基础API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
listComments() |
无 | void | 列出所有评论 | $comments->listComments() |
pageNav() |
无 | void | 评论分页导航 | $comments->pageNav() |
4.3 评论信息API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
permalink() |
无 | void | 输出评论所在文章链接 | $comments->permalink() |
author() |
\(autoLink` | void | 输出评论作者 | `\)comments->author()$comments->author(false) |
|||
excerpt() |
\(length, \)suffix |
void | 输出评论摘要 | \(comments->excerpt(10, '[...]')`<br>`\)comments->excerpt(35, '...') |
content() |
无 | void | 输出评论内容 | $comments->content() |
date() |
\(format` | void | 输出评论日期 | `\)comments->date('F jS, Y')\(comments->date('h:i a')`<br>`\)comments->date('Y-m-d H:i') |
4.4 评论进阶API
| 方法/属性 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
theId() |
无 | void | 输出评论唯一ID | $comments->theId() |
sequence() |
无 | int | 评论所在楼层 | $comments->sequence() |
responseUrl() |
无 | void | 回复地址 | $comments->responseUrl() |
responseId() |
无 | void | 回复框ID | $comments->responseId() |
trackbackUrl() |
无 | void | Trackback地址 | $comments->trackbackUrl() |
gravatar() |
\(size, \)default, \(rating, \)alt |
void | 头像显示 | \(comments->gravatar(40, '', '', 'avatar')`<br>`\)comments->gravatar('40', '') |
reply() |
\(word` | void | 回复按钮 | `\)comments->reply()$comments->reply('回复') |
|||
threadedComments() |
\(options` | void | 嵌套评论 | `\)comments->threadedComments($options) |
|||
children |
属性 | mixed | 子评论属性 | if ($comments->children) { ... } |
levels |
属性 | int | 评论层级 | $comments->levels |
levelsAlt() |
\(odd, \)even |
void | 层级交替样式 | $comments->levelsAlt(' comment-level-odd', ' comment-level-even') |
alt() |
\(odd, \)even |
void | 交替样式 | $comments->alt(' comment-odd', ' comment-even') |
authorId |
属性 | int | 作者ID | $comments->authorId |
ownerId |
属性 | int | 文章作者ID | $comments->ownerId |
4.5 评论回复API
| 方法 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
cancelReply() |
无 | void | 输出取消回复链接 | $comments->cancelReply() |
4.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(); ?>
</div>
<?php if ($comments->children) { ?>
<div class="comment-children">
<?php \(comments->threadedComments(\)options); ?>
</div>
<?php } ?>
</li>
<?php } ?>五、Widget类 API
5.1 页面列表 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Contents\Page\Rows |
alloc() |
无 | 分配页面列表Widget | \Widget\Contents\Page\Rows::alloc()->to($pages) |
| - | next() |
无 | 移动到下一页 | $pages->next() |
| - | slug |
属性 | 页面缩略名 | $pages->slug |
| - | permalink() |
无 | 输出页面链接 | $pages->permalink() |
| - | title() |
无 | 输出页面标题 | $pages->title() |
页面导航示例:
<nav id="nav-menu" class="clearfix" role="navigation">
<a<?php if ($this->is('index')): ?> class="current"<?php endif; ?>
href="<?php $this->options->siteUrl(); ?>">首页</a>
<?php \Widget\Contents\Page\Rows::alloc()->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; ?>
</nav>5.2 最新文章 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Contents\Post\Recent |
alloc() |
无 | 分配最新文章Widget | \Widget\Contents\Post\Recent::alloc()->parse('<li><a href="{permalink}">{title}</a></li>') |
| - | parse() |
$pattern |
解析输出格式 | 参见示例 |
占位符:
{permalink}: 文章链接{title}: 文章标题
5.3 最近评论 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Comments\Recent |
alloc() |
无 | 分配最近评论Widget | \Widget\Comments\Recent::alloc()->to($comments) |
最近评论示例:
<ul>
<?php \Widget\Comments\Recent::alloc()->to($comments); ?>
<?php while ($comments->next()): ?>
<li>
<a href="<?php \(comments->permalink(); ?>"><?php \)comments->author(false); ?></a>:
<?php $comments->excerpt(35, '...'); ?>
</li>
<?php endwhile; ?>
</ul>5.4 分类 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Metas\Category\Rows |
alloc() |
无 | 分配分类Widget | \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list') |
占位符(parse模式):
{permalink}: 分类链接{name}: 分类名称{count}: 该分类下文章数{description}: 分类描述
分类列表示例:
// 方法一:使用listCategories
<?php \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list'); ?>
// 方法二:使用parse(带描述)
<?php \Widget\Metas\Category\Rows::alloc()
->parse('<li><a href="{permalink}" title="{description}">{name}</a> ({count})</li>'); ?>
// 方法三:获取分类描述
<?php echo $this->getDescription(); ?>5.5 归档 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Contents\Post\Date |
alloc() |
$options |
分配归档Widget | \Widget\Contents\Post\Date::alloc('type=month&format=F Y')->parse('<li><a href="{permalink}">{date}</a></li>') |
参数说明:
type: 归档类型 (year/month/day)format: 日期格式
占位符:
{permalink}: 归档链接{date}: 归档日期
5.6 自定义分类文章 Widget
| 类 | 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|---|
\Widget\Archive |
alloc() |
\(name, \)params |
自定义查询Widget | \(this->widget('Widget_Archive@myCustomCategory', 'type=category', 'mid=1')->to(\)categoryPosts) |
调用指定分类文章示例:
<?php
\(this->widget('Widget_Archive@myCustomCategory', 'type=category', 'mid=1')->to(\)categoryPosts);
if ($categoryPosts->have()):
while ($categoryPosts->next()):
// 显示文章
<a href="<?php \(categoryPosts->permalink(); ?>"><?php \)categoryPosts->title(); ?></a>
endwhile;
endif;
?>5.7 Widget占位符汇总表
| 占位符 | 说明 | 适用Widget |
|---|---|---|
{permalink} |
链接地址 | Post\Recent, Post\Date, Category, Page |
{title} |
标题 | Post\Recent, Post\Date, Page |
{date} |
日期 | Post\Date |
{name} |
名称 | Category |
{count} |
数量 | Category |
{description} |
描述 | Category |
六、全局函数 API
6.1 翻译函数
| 函数 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
_e() |
$string |
void | 输出翻译文本 | _e('首页')_e('搜索')_e('暂无评论')_e('阅读剩余部分') |
_t() |
$string |
string | 返回翻译文本 | _t('暂无评论')_t('显示最新文章') |
6.2 主题配置函数
| 函数 | 参数 | 返回值 | 说明 | 使用示例 |
|---|---|---|---|---|
themeConfig() |
\(form` | void | 主题配置回调 | `function themeConfig(\)form) { ... } |
|||
themeFields() |
\(layout` | void | 文章字段配置 | `function themeFields(\)layout) { ... } |
|||
themeInit() |
\(archive` | void | 主题初始化 | `function themeInit(\)archive) { ... } |
themeInit示例:
function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 10; // 自定义分类文章显示条数
}
}七、表单元素 API (主题配置)
7.1 文本输入框
new \Typecho\Widget\Helper\Form\Element\Text(
'name', // 字段名
null, // 值
null, // 默认值
_t('标题'), // 标签
_t('描述') // 说明
)示例:
$logoUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'logoUrl',
null,
null,
_t('站点 LOGO 地址'),
_t('在这里填入一个图片 URL 地址, 以在网站标题前加上一个 LOGO')
);
\(form->addInput(\)logoUrl->addRule('url', _t('请填写一个合法的URL地址')));7.2 复选框
new \Typecho\Widget\Helper\Form\Element\Checkbox(
'name', // 字段名
[ // 选项
'ShowRecentPosts' => _t('显示最新文章'),
'ShowRecentComments' => _t('显示最近回复'),
'ShowCategory' => _t('显示分类'),
'ShowArchive' => _t('显示归档'),
'ShowOther' => _t('显示其它杂项')
],
['ShowRecentPosts', 'ShowRecentComments'], // 默认选中
_t('侧边栏显示') // 标签
)示例:
$sidebarBlock = new \Typecho\Widget\Helper\Form\Element\Checkbox(
'sidebarBlock',
[
'ShowRecentPosts' => _t('显示最新文章'),
'ShowRecentComments' => _t('显示最近回复'),
'ShowCategory' => _t('显示分类'),
'ShowArchive' => _t('显示归档'),
'ShowOther' => _t('显示其它杂项')
],
['ShowRecentPosts', 'ShowRecentComments', 'ShowCategory', 'ShowArchive', 'ShowOther'],
_t('侧边栏显示')
);
\(form->addInput(\)sidebarBlock->multiMode());7.3 表单方法
| 方法 | 参数 | 说明 | 使用示例 |
|---|---|---|---|
addInput() |
\(element` | 添加输入元素 | `\)form->addInput($logoUrl) |
||
addRule() |
\(rule, \)message |
添加验证规则 | $logoUrl->addRule('url', _t('请填写一个合法的URL地址')) |
multiMode() |
无 | 启用多选模式 | $sidebarBlock->multiMode() |
7.4 完整主题配置示例
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
function themeConfig($form)
{
// Logo设置
$logoUrl = new \Typecho\Widget\Helper\Form\Element\Text(
'logoUrl',
null,
null,
_t('站点 LOGO 地址'),
_t('在这里填入一个图片 URL 地址, 以在网站标题前加上一个 LOGO')
);
\(form->addInput(\)logoUrl->addRule('url', _t('请填写一个合法的URL地址')));
// 侧边栏设置
$sidebarBlock = new \Typecho\Widget\Helper\Form\Element\Checkbox(
'sidebarBlock',
[
'ShowRecentPosts' => _t('显示最新文章'),
'ShowRecentComments' => _t('显示最近回复'),
'ShowCategory' => _t('显示分类'),
'ShowArchive' => _t('显示归档'),
'ShowOther' => _t('显示其它杂项')
],
['ShowRecentPosts', 'ShowRecentComments', 'ShowCategory', 'ShowArchive', 'ShowOther'],
_t('侧边栏显示')
);
\(form->addInput(\)sidebarBlock->multiMode());
}
?>八、自定义模板
8.1 自定义首页模板
在模板目录下创建文件(如 home.php),在文件开头添加:
<?php
/**
* 自定义首页模板
*
* @package index
*/
?>然后进入后台『设置』-『文章』页面,选择"站点首页"中的"直接调用[home.php]模板文件",保存即可。
8.2 自定义页面模板
在模板目录下创建文件,在文件开头添加:
<?php
/**
* 自定义页面模板
*
* @package custom
*/
?>其中 @package custom 是必须的,然后进入 Typecho 后台在『创建页面』的【展开高级选项】里就可以看到并选择该模板。
8.3 自定义分类模板
方法一: 直接在模板目录下建立一个名为 category 的目录,然后在里面放上以分类缩略名为文件名的 php 文件。例如访问缩略名为 default 的分类时,会自动调用 category/default.php。
方法二: 在模板文件中使用 is 语法判断:
<?php if ($this->is('category', 'default')): ?>
<!-- 默认分类模板 -->
<?php endif; ?>
<?php if ($this->is('category', 'category2')): ?>
<!-- 分类2模板 -->
<?php endif; ?>8.4 自定义404页面
- 制作一个HTML页面,命名为
404.php - 把页面放到当前模板目录下即可
8.5 自定义错误页面
- 创建一个php文件(有两个变量可以直接使用:
\(code`和`\)message,分别代表错误代码和错误信息) - 把文件传到服务器某个路径
- 打开
config.inc.php,加入:define('__TYPECHO_EXCEPTION_FILE__', '你的文件路径');
九、进阶功能
9.1 面包屑导航
<div class="crumbs_patch">
<a href="<?php $this->options->siteUrl(); ?>">首页</a> »
<?php if ($this->is('index')): ?>
最新文章
<?php elseif ($this->is('post')): ?>
<?php \(this->category(); ?> » <?php \)this->title() ?>
<?php else: ?>
<?php $this->archiveTitle(' » ', '', ''); ?>
<?php endif; ?>
</div>9.2 相关文章
<?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>9.3 分离评论和引用通告
<!-- 只显示评论 -->
<?php \(this->comments('comment')->to(\)comments); ?>
<?php if ($comments->have()): ?>
<h3>评论</h3>
<ol>
<?php while ($comments->next()): ?>
<li id="<?php $comments->theId() ?>">
<div class="comment_data">
<?php $comments->gravatar(32, '', '', 'avatar'); ?>
<span><?php $comments->author() ?></span> 说:<br />
<?php \(comments->date('F jS, Y'); ?> at <?php \)comments->date('h:i a'); ?>
</div>
<div class="comment_text"><?php $comments->content() ?></div>
</li>
<?php endwhile; ?>
</ol>
<?php endif; ?>
<!-- 只显示Pingback -->
<?php \(this->comments('pingback')->to(\)pingbacks); ?>
<?php if ($pingbacks->have()): ?>
<h3>Pingbacks</h3>
<ol>
<?php while ($pingbacks->next()): ?>
<li id="<?php $pingbacks->theId() ?>">
<?php \(pingbacks->author() ?> <?php \)pingbacks->date('F jS, Y'); ?>
</li>
<?php endwhile; ?>
</ol>
<?php endif; ?>十、文件功能映射表
| 文件 | 主要功能 | 涉及API类型 | 主要API示例 |
|---|---|---|---|
404.php |
404错误页面 | 模板加载、翻译、表单提交 | $this->need(), _e(), 搜索表单 |
comments.php |
评论展示与提交 | 评论API、用户API、表单 | \(this->comments()`, `\)comments->author(), \(this->allow()`, `\)this->commentUrl() |
footer.php |
页脚输出 | 选项API、日期函数 | \(this->options->siteUrl()`, `\)this->options->title(), $this->footer() |
functions.php |
主题配置与自定义函数 | 表单API、自定义函数 | themeConfig(), themeFields(), Form\Element\Text |
header.php |
页头与导航 | 选项API、页面判断、Widget | \(this->options->charset()`, `\)this->archiveTitle(), $this->is(), \Widget\Contents\Page\Rows |
index.php |
文章列表 | 文章循环、内容API、分页 | \(this->have()`, `\)this->next(), \(this->content()`, `\)this->pageNav() |
page.php |
独立页面 | 文章内容、模板加载 | \(this->content()`, `\)this->need() |
post.php |
文章详情 | 文章内容、标签、导航 | \(this->content()`, `\)this->tags(), \(this->thePrev()`, `\)this->theNext() |
sidebar.php |
侧边栏小工具 | Widget API、用户API | \Widget\Contents\Post\Recent, \Widget\Comments\Recent, \Widget\Metas\Category\Rows, $this->user->hasLogin() |
十一、常用代码片段
11.1 文章信息显示
<div class="post-meta">
<li>作者: <a href="<?php \(archive->author->permalink(); ?>"><?php \)archive->author(); ?></a></li>
<li>时间: <time datetime="<?php \(archive->date('c'); ?>"><?php \)archive->date(); ?></time></li>
<li>分类: <?php $archive->category(','); ?></li>
<li>评论: <a href="<?php \(archive->permalink() ?>#comments"><?php \)archive->commentsNum(_t('暂无评论'), _t('1 条评论'), _t('%d 条评论')); ?></a></li>
</div>11.2 侧边栏小工具
<!-- 最新文章 -->
<section class="widget">
<h3 class="widget-title">最新文章</h3>
<ul class="widget-list">
<?php \Widget\Contents\Post\Recent::alloc()
->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
</ul>
</section>
<!-- 最近评论 -->
<section class="widget">
<h3 class="widget-title">最近回复</h3>
<ul class="widget-list">
<?php \Widget\Comments\Recent::alloc()->to($comments); ?>
<?php while ($comments->next()): ?>
<li>
<a href="<?php \(comments->permalink(); ?>"><?php \)comments->author(false); ?></a>:
<?php $comments->excerpt(35, '...'); ?>
</li>
<?php endwhile; ?>
</ul>
</section>
<!-- 分类列表 -->
<section class="widget">
<h3 class="widget-title">分类</h3>
<?php \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list'); ?>
</section>
<!-- 归档 -->
<section class="widget">
<h3 class="widget-title">归档</h3>
<ul class="widget-list">
<?php \Widget\Contents\Post\Date::alloc('type=month&format=F Y')
->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
</ul>
</section>11.3 评论表单
<?php if ($this->allow('comment')): ?>
<div id="<?php $this->respondId(); ?>" class="respond">
<div class="cancel-comment-reply">
<?php $comments->cancelReply(); ?>
</div>
<h3 id="response">添加新评论</h3>
<form method="post" action="<?php $this->commentUrl() ?>" id="comment-form" role="form">
<?php if ($this->user->hasLogin()): ?>
<p>登录身份: <a href="<?php $this->options->profileUrl(); ?>">
<?php $this->user->screenName(); ?>
</a>.
<a href="<?php $this->options->logoutUrl(); ?>" title="Logout">退出 »</a></p>
<?php else: ?>
<p>
<label for="author" class="required">称呼</label>
<input type="text" name="author" id="author" class="text"
value="<?php $this->remember('author'); ?>" required/>
</p>
<p>
<label for="mail" class="required">Email</label>
<input type="email" name="mail" id="mail" class="text"
value="<?php \(this->remember('mail'); ?>"<?php if (\)this->options->commentsRequireMail): ?> required<?php endif; ?> />
</p>
<p>
<label for="url">网站</label>
<input type="url" name="url" id="url" class="text" placeholder="http://"
value="<?php \(this->remember('url'); ?>"<?php if (\)this->options->commentsRequireUrl): ?> required<?php endif; ?> />
</p>
<?php endif; ?>
<p>
<label for="textarea" class="required">内容</label>
<textarea rows="8" cols="50" name="text" id="textarea" class="textarea"
required><?php $this->remember('text'); ?></textarea>
</p>
<p>
<button type="submit" class="submit">提交评论</button>
</p>
</form>
</div>
<?php else: ?>
<h3>评论已关闭</h3>
<?php endif; ?>十二、注意事项
-
安全防护:所有模板文件开头必须包含:
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?> -
主题注释:模板信息注释格式:
/** * 主题描述 * * @package 主题名 * @author 作者名 * @version 版本号 * @link 作者网站 */ -
自定义模板:
- 自定义首页:
@package index - 自定义页面:
@package custom
- 自定义首页:
-
主题初始化:
themeInit()函数在版本 Revision: 1466 (2010-06-28) 及以后有效 -
嵌套评论:使用自定义评论函数时,记得添加审核提示:
<?php $singleCommentOptions->commentStatus(); ?> -
Widget占位符:不同Widget支持的占位符不同,请参考具体文档
附录:常量与变量
常用常量
| 常量 | 说明 |
|---|---|
__TYPECHO_ROOT_DIR__ |
Typecho根目录 |
__TYPECHO_EXCEPTION_FILE__ |
自定义错误页面路径 |
常用变量
| 变量名 | 类型 | 说明 |
|---|---|---|
$this |
Object | 当前上下文对象 |
$comments |
Object | 评论对象 |
$pages |
Object | 页面对象 |
$form |
Object | 表单对象 |
$archive |
Object | 存档对象(函数参数) |
文档结束
本文档整理了Typecho主题开发中所有可用的API,涵盖了模板开发的核心功能。如有遗漏或错误,欢迎反馈补充。
最后更新时间: 2026年2月3日
暂无评论
还没有评论,快来抢沙发吧!