Typecho PHP 模板 API 完整参考
本文档汇集了 Typecho 主题和插件开发中所有可用的 PHP 模板 API 及函数。
目录
- Widget\Archive - 主题核心 API
- Widget\Base\Contents - 内容 API
- Widget\Base\Comments - 评论 API
- Widget\Options - 系统配置 API
- Utils\Helper - 辅助工具 API
- Hook 系统完整列表
Widget\Archive - 主题核心 API
Widget\Archive 是主题开发中最核心的 API,提供了内容循环、分页、页面判断等功能。
循环控制
have() - 检查是否有内容
while ($this->have()) {
// 循环内容
}返回值: bool - 是否有更多内容
next() - 移动到下一项
while ($this->have()) {
$this->next();
// 处理当前项
}内容输出
title(\(length = 0, \)trim = '...') - 输出标题
$this->title(); // 完整标题
$this->title(50, '...'); // 截取标题参数:
$length- 截取长度,0 表示不截取$trim- 截取后缀
content($more = null) - 输出内容
$this->content(); // 完整内容
$this->content('阅读全文'); // 带 more 标签截取参数:
$more- more 标签的链接文字
excerpt(\(length = 100, \)trim = '...') - 输出摘要
$this->excerpt(); // 默认 100 字
$this->excerpt(200, '——'); // 自定义截取date($format = null) - 输出日期
$this->date(); // 使用后台设置格式
$this->date('Y-m-d H:i:s'); // 自定义格式分类与标签
category(\(split = ',', \)link = true, $default = null) - 输出分类
$this->category(); // 逗号分隔,带链接
$this->category(' / ', false); // 斜线分隔,无链接
$this->category(',', true, '未分类'); // 带默认值tags(\(split = ',', \)link = true, $default = null) - 输出标签
$this->tags(); // 逗号分隔,带链接
$this->tags(' ', false); // 空格分隔,纯文本directory(\(split = '/', \)link = true, $default = null) - 输出多级分类
$this->directory(' > '); // 输出完整分类路径作者信息
author($item = 'screenName') - 输出作者信息
$this->author(); // 昵称
$this->author('url'); // 作者主页
$this->author('mail'); // 作者邮箱可用字段: screenName, url, mail, uid
评论相关
commentsNum(...$args) - 输出评论数
$this->commentsNum(); // 直接输出数字
$this->commentsNum('%d 评论'); // 带单位
$this->commentsNum('暂无评论', '1 条评论', '%d 条评论'); // 复数处理comments() - 获取评论归档对象
\(comments = \)this->comments();
while ($comments->have()) {
$comments->next();
// 处理评论
}返回值: \Widget\Comments\Archive
pings() - 获取引用归档对象
\(pings = \)this->pings();返回值: \Widget\Comments\Ping
分页导航
pageNav(\(prev = '«', \)next = '»', \(splitPage = 3, \)splitWord = '...', $template = '') - 完整分页
$this->pageNav('« 上一页', '下一页 »', 3, '...');pageLink(\(word = '« Previous Entries', \)page = 'prev') - 简单分页
$this->pageLink('« 上一页', 'prev'); // 上一页
$this->pageLink('下一页 »', 'next'); // 下一页相关内容
theNext(\(format = '%s', \)default = null, $custom = []) - 下一篇
$this->theNext('<a href="%s">下一篇: %s</a>', '没有下一篇了');thePrev(\(format = '%s', \)default = null, $custom = []) - 上一篇
$this->thePrev('<a href="%s">上一篇: %s</a>', '没有上一篇了');related(\(limit = 5, \)type = null) - 相关文章
// 获取相关文章
\(related = \)this->related(5); // 默认按标签
\(related = \)this->related(5, 'author'); // 按作者
while ($related->have()) {
$related->next();
// 处理相关文章
}页面判断
is(\(archiveType, \)archiveSlug = null) - 判断页面类型
$this->is('index'); // 首页
$this->is('single'); // 单篇文章/页面
$this->is('post'); // 文章
$this->is('page'); // 独立页面
$this->is('category'); // 分类页
$this->is('category', 'default'); // 指定分类
$this->is('tag'); // 标签页
$this->is('search'); // 搜索页
$this->is('archive'); // 归档页
$this->is('author'); // 作者页
$this->is('feed'); // 订阅输出模板操作
need($fileName) - 加载模板文件
$this->need('header.php'); // 加载头部
$this->need('sidebar.php'); // 加载侧边栏
$this->need('footer.php'); // 加载底部header($rule = null) - 输出头部元数据
$this->header(); // 输出默认头部
$this->header('keywords=&description='); // 自定义输出项footer() - 输出页脚
$this->footer(); // 输出页脚(支持插件扩展)归档信息
archiveTitle(\(defines = null, \)before = ' » ', $end = '') - 输出归档标题
$this->archiveTitle(); // 默认格式
$this->archiveTitle([
'category' => '分类 %s 下的文章',
'search' => '搜索: %s',
'tag' => '标签: %s',
'author' => '作者: %s'
], ' - ');keywords(\(split = ',', \)default = '') - 输出关键词
$this->keywords(); // 逗号分隔
$this->keywords(' ', '默认关键词'); // 自定义分隔符remember(\(cookieName, \)return = false) - 输出 Cookie 记忆值
$this->remember('author'); // 记忆的作者名
$this->remember('mail'); // 记忆的邮箱
$this->remember('url'); // 记忆的网址附件
attachments(\(limit = 0, \)offset = 0) - 获取附件
\(attachments = \)this->attachments();
while ($attachments->have()) {
$attachments->next();
echo $attachments->url;
}属性列表
归档信息:
$this->archiveTitle- 归档标题$this->archiveSlug- 归档缩略名$this->archiveType- 归档类型$this->archiveUrl- 归档 URL$this->archiveDescription- 归档描述$this->archiveKeywords- 归档关键词
Feed 地址:
$this->archiveFeedUrl- RSS 2.0$this->archiveFeedRssUrl- RSS 1.0$this->archiveFeedAtomUrl- Atom 1.0
分页信息:
$this->currentPage- 当前页码$this->total- 总记录数$this->getTotalPage()- 总页数
主题路径:
$this->themeDir- 主题目录$this->themeFile- 当前模板文件
Widget\Base\Contents - 内容 API
文章/页面内容的详细属性和方法。
基础属性
$this->cid; // 内容 ID
$this->title; // 标题
$this->slug; // 缩略名
$this->created; // 创建时间戳
$this->modified; // 修改时间戳
$this->text; // 原始内容
$this->order; // 排序
$this->authorId; // 作者 ID
$this->template; // 自定义模板
$this->type; // 类型(post/page/attachment)
$this->status; // 状态
$this->password; // 密码保护
$this->commentsNum; // 评论数
$this->allowComment; // 允许评论
$this->allowPing; // 允许引用
$this->allowFeed; // 允许 Feed
$this->parent; // 父级 ID动态属性
$this->permalink; // 永久链接
$this->url; // URL(同 permalink)
$this->path; // 路径
$this->feedUrl; // RSS 2.0
$this->feedRssUrl; // RSS 1.0
$this->feedAtomUrl; // Atom 1.0
$this->theId; // 锚点 ID(type-cid)
$this->respondId; // 回复框 ID
$this->commentUrl; // 评论提交地址
$this->trackbackUrl; // Trackback 地址
$this->responseUrl; // 回复地址内容属性
$this->content; // 解析后的内容
$this->excerpt; // 摘要(带 more 标签处理)
$this->plainExcerpt; // 纯文本摘要
$this->summary; // 第一段摘要
$this->fields; // 自定义字段对象
$this->attachment; // 附件信息(仅 attachment 类型)日期属性
$this->date; // Date 对象
$this->dateWord; // 词义化日期(如"3小时前")
$this->year; // 年份
$this->month; // 月份
$this->day; // 日期分类标签
$this->categories; // 分类数组
$this->tags; // 标签数组
$this->directory; // 多级目录数组作者信息
$this->author; // 作者对象(Widget\Users\Author)
$this->author->uid; // 作者 ID
$this->author->screenName; // 昵称
$this->author->url; // 作者主页
$this->author->mail; // 作者邮箱方法
allow(...$permissions) - 检查权限
$this->allow('comment'); // 允许评论
$this->allow('ping'); // 允许引用
$this->allow('feed'); // 允许 Feed
$this->allow('edit'); // 允许编辑
$this->allow('comment', 'edit'); // 多个权限返回值: bool
isMarkdown - 是否为 Markdown
if ($this->isMarkdown) {
// 是 Markdown 格式
}hidden - 是否密码保护
if ($this->hidden) {
// 内容被密码保护
}Widget\Base\Comments - 评论 API
评论系统的属性和方法。
基础属性
$this->coid; // 评论 ID
$this->cid; // 所属内容 ID
$this->created; // 创建时间戳
$this->author; // 作者名
$this->authorId; // 作者 ID(注册用户)
$this->ownerId; // 内容作者 ID
$this->mail; // 邮箱
$this->url; // 网址
$this->ip; // IP 地址
$this->agent; // User Agent
$this->text; // 评论内容
$this->type; // 类型(comment/pingback/trackback)
$this->status; // 状态
$this->parent; // 父评论 ID
$this->commentPage; // 所在评论页动态属性
$this->date; // Date 对象
$this->dateWord; // 词义化日期
$this->theId; // 锚点 ID(type-coid)
$this->permalink; // 评论链接
$this->title; // 所属内容标题
$this->content; // 解析后的评论内容
$this->parentContent; // 所属内容对象方法
date($format = null) - 输出日期
$this->date(); // 默认格式
$this->date('Y-m-d H:i:s'); // 自定义格式author(\(autoLink = null, \)noFollow = null) - 输出作者
$this->author(); // 默认带链接
$this->author(false); // 不带链接
$this->author(true, true); // 带 nofollowgravatar(\(size = 32, \)default = null, $highRes = false) - 输出头像
$this->gravatar(); // 默认 32px
$this->gravatar(64); // 64px
$this->gravatar(64, 'mm', true); // 高分辨率支持excerpt(\(length = 100, \)trim = '...') - 输出摘要
$this->excerpt(); // 默认 100 字
$this->excerpt(50); // 50 字mail($link = false) - 输出邮箱
$this->mail(); // 纯邮箱
$this->mail(true); // mailto: 链接Widget\Options - 系统配置 API
全局选项和系统配置。
URL 方法
siteUrl($path = null) - 网站地址
$this->options->siteUrl(); // http://example.com
$this->options->siteUrl('about/'); // http://example.com/about/index($path = null) - 首页地址
$this->options->index(); // 网站首页themeUrl(\(path = null, \)theme = null) - 主题地址
$this->options->themeUrl(); // 当前主题 URL
$this->options->themeUrl('style.css'); // 主题文件
$this->options->themeUrl(null, 'default'); // 指定主题pluginUrl($path = null) - 插件地址
$this->options->pluginUrl(); // 插件目录
$this->options->pluginUrl('MyPlugin/'); // 指定插件adminUrl(\(path = null, \)return = false) - 后台地址
$this->options->adminUrl(); // 后台首页
$this->options->adminUrl('write-post.php'); // 写文章页
\(url = \)this->options->adminUrl('manage-posts.php', true); // 返回不输出Feed 地址
$this->options->feedUrl; // RSS 2.0
$this->options->feedRssUrl; // RSS 1.0
$this->options->feedAtomUrl; // Atom 1.0
$this->options->commentsFeedUrl; // 评论 RSS 2.0
$this->options->commentsFeedRssUrl; // 评论 RSS 1.0
$this->options->commentsFeedAtomUrl; // 评论 Atom 1.0系统属性
基本设置
$this->options->title; // 网站标题
$this->options->description; // 网站描述
$this->options->keywords; // 网站关键词
$this->options->theme; // 当前主题
$this->options->siteUrl; // 网站地址
$this->options->siteDomain; // 网站域名内容设置
$this->options->pageSize; // 每页文章数
$this->options->postDateFormat; // 文章日期格式
$this->options->commentDateFormat; // 评论日期格式
$this->options->frontPage; // 首页显示(recent/page:X/file:X)
$this->options->frontArchive; // 归档作为首页评论设置
$this->options->commentsListSize; // 每页评论数
$this->options->commentsPageSize; // 评论分页大小
$this->options->commentsShowCommentOnly; // 只显示评论
$this->options->commentsThreaded; // 启用嵌套评论
$this->options->commentsOrder; // 评论排序(ASC/DESC)
$this->options->commentsMarkdown; // 支持 Markdown
$this->options->commentsShowUrl; // 显示评论者网址
$this->options->commentsUrlNofollow; // 添加 nofollow
$this->options->commentsAvatar; // 显示头像
$this->options->commentsAvatarRating; // 头像评级
$this->options->commentsPageBreak; // 评论分页
$this->options->commentsHTMLTagAllowed; // 允许的 HTML 标签
$this->options->commentsAntiSpam; // 反垃圾评论
$this->options->commentsAutoClose; // 自动关闭评论
$this->options->commentsPostTimeout; // 关闭评论天数
$this->options->commentsMaxNestingLevels;// 最大嵌套层数其他设置
$this->options->allowRegister; // 允许注册
$this->options->allowXmlRpc; // XML-RPC 支持
$this->options->feedFullText; // Feed 输出全文
$this->options->time; // 当前时间戳
$this->options->timezone; // 时区
$this->options->serverTimezone; // 服务器时区
$this->options->generator; // Generator 信息
$this->options->version; // 版本号
$this->options->software; // 软件名称插件配置
plugin($pluginName) - 获取插件配置
\(config = \)this->options->plugin('HelloWorld');
echo $config->word; // 访问配置项返回值: \Typecho\Config
personalPlugin($pluginName) - 获取个人插件配置
\(config = \)this->options->personalPlugin('HelloWorld');其他方法
themeFile(\(theme, \)file = '') - 获取主题文件路径
\(path = \)this->options->themeFile('default', 'index.php');pluginDir($plugin = null) - 获取插件目录
\(dir = \)this->options->pluginDir('HelloWorld');adminStaticUrl(\(type, \)file = null, $return = false) - 后台静态文件地址
$this->options->adminStaticUrl('css', 'style.css');Utils\Helper - 辅助工具 API
插件开发辅助工具类。
路由管理
addRoute(\(name, \)url, \(widget, \)action = null, $after = null) - 添加路由
use Utils\Helper;
Helper::addRoute(
'myplugin-api', // 路由名称
'/api/myplugin/[action:alpha]', // 路由路径
'MyPlugin_Action', // 处理 Widget
'api', // 动作方法
'post' // 在 post 路由之后
);removeRoute($name) - 删除路由
Helper::removeRoute('myplugin-api');Action 扩展
addAction(\(actionName, \)widgetName) - 添加 Action
Helper::addAction('my-action', 'MyPlugin_Action');
// 访问: /action/my-actionremoveAction($actionName) - 删除 Action
Helper::removeAction('my-action');后台菜单
addMenu($menuName) - 添加菜单
$menuIndex = Helper::addMenu('MyPlugin');
// 返回菜单索引,用于添加子面板removeMenu($menuName) - 删除菜单
$menuIndex = Helper::removeMenu('MyPlugin');后台面板
addPanel(\(index, \)fileName, \(title, \)subTitle, \(level, \)hidden = false, $addLink = '') - 添加面板
$menuIndex = Helper::addMenu('MyPlugin');
Helper::addPanel(
$menuIndex, // 菜单索引
'panel.php', // 面板文件
'插件设置', // 标题
'配置插件参数', // 副标题
'administrator', // 权限
false, // 是否隐藏
'新增数据' // 新增链接
);removePanel(\(index, \)fileName) - 删除面板
$menuIndex = Helper::removeMenu('MyPlugin');
Helper::removePanel($menuIndex, 'panel.php');工具方法
options() - 获取 Options 对象
$options = Helper::options();
echo $options->title;返回值: \Widget\Options
security() - 获取 Security 对象
$security = Helper::security();
\(token = \)security->getToken($url);返回值: \Widget\Security
url($fileName) - 获取面板 URL
$url = Helper::url('panel.php');
// 输出: /admin/extending.php?panel=panel.phpwidgetById(\(table, \)pkId) - 根据 ID 获取 Widget
$post = Helper::widgetById('contents', 1);
$comment = Helper::widgetById('comments', 1);
$category = Helper::widgetById('metas', 1);
$user = Helper::widgetById('users', 1);setOption(\(name, \)value) - 设置系统选项
Helper::setOption('plugin:MyPlugin', ['key' => 'value']);configPlugin(\(pluginName, \)settings, $isPersonal = false) - 配置插件
Helper::configPlugin('MyPlugin', [
'option1' => 'value1',
'option2' => 'value2'
]);removePlugin($pluginName) - 强行删除插件
Helper::removePlugin('MyPlugin');评论辅助
replyLink(\(theId, \)coid, \(word = 'Reply', \)formId = 'respond', $style = 2) - 评论回复链接
Helper::replyLink('comment-123', 456, '回复');cancelCommentReplyLink(\(word = 'Cancel', \)formId = 'respond') - 取消回复链接
Helper::cancelCommentReplyLink('取消回复');threadedCommentsScript() - 输出嵌套评论脚本
Helper::threadedCommentsScript();requestService(\(method, ...\)params) - 请求异步服务
Helper::requestService('methodName', \(param1, \)param2);lang($domain) - 导入语言项
Helper::lang('MyPlugin');Hook 系统完整列表
Typecho 的 Hook 系统允许插件在特定位置扩展功能。
核心系统 Hook
index.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
begin |
call | 无 | 系统启动时调用 |
end |
call | 无 | 系统结束时调用 |
admin/common.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
begin |
call | 无 | 后台公共初始化 |
admin/header.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
header |
filter | $header |
过滤后台头部 CSS |
admin/footer.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
begin |
call | 无 | 后台底部开始 |
end |
call | 无 | 后台底部结束 |
admin/menu.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
navBar |
call | 无 | 后台导航栏扩展 |
内容编辑 Hook
admin/write-post.php / admin/write-page.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
content |
call | \(post/\)page |
内容编辑区域 |
option |
call | \(post/\)page |
选项侧边栏 |
advanceOption |
call | \(post/\)page |
高级选项 |
richEditor |
call+trigger | \(post/\)page |
富文本编辑器替换 |
bottom |
call | \(post/\)page |
页面底部扩展 |
admin/editor-js.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
markdownEditor |
call | $content |
Markdown 编辑器增强 |
admin/write-js.php
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
write |
call | 无 | 编写页面 JS 增强 |
Widget 内容展示 Hook
Widget\Archive
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
select |
filter | $query |
过滤数据库查询 |
handleInit |
call | $archive |
内容初始化后 |
handle |
call | \(archive`, `\)type |
自定义内容处理 |
headerOptions |
filter | \(options`, `\)archive |
过滤头部选项 |
header |
call | \(header`, `\)archive |
头部渲染时 |
footer |
call | $archive |
底部渲染时 |
beforeRender |
call | $archive |
模板渲染前 |
afterRender |
call | $archive |
模板渲染后 |
indexHandle |
call | \(archive`, `\)select |
首页处理 |
singleHandle |
call | \(archive`, `\)select |
单篇处理 |
categoryHandle |
call | \(archive`, `\)select |
分类处理 |
tagHandle |
call | \(archive`, `\)select |
标签处理 |
authorHandle |
call | \(archive`, `\)select |
作者处理 |
dateHandle |
call | \(archive`, `\)select |
日期处理 |
searchHandle |
call | \(archive`, `\)select |
搜索处理 |
error404Handle |
call | \(archive`, `\)select |
404 处理 |
search |
call | \(archive`, `\)keywords |
搜索操作 |
feedItem |
filter | $item |
过滤 RSS/Atom 项目 |
pageNav |
call | 多个参数 | 分页导航 |
Widget\Base\Contents
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
filter |
filter | \(row`, `\)widget |
过滤内容行数据 |
title |
filter | \(title`, `\)widget |
过滤标题 |
excerpt |
filter | \(excerpt`, `\)widget |
过滤摘要 |
excerptEx |
filter | \(excerpt`, `\)widget |
扩展摘要过滤 |
markdown |
filter | \(text`, `\)widget |
过滤 Markdown |
autoP |
filter | \(text`, `\)widget |
过滤自动段落 |
content |
filter | \(content`, `\)widget |
过滤内容 |
contentEx |
filter | \(content`, `\)widget |
扩展内容 |
isFieldReadOnly |
call+trigger | $fieldName |
检查字段只读 |
getDefaultFieldItems |
filter | $items |
添加默认字段 |
Widget\Base\Comments
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
filter |
filter | \(row`, `\)widget |
过滤评论行数据 |
gravatar |
call+trigger | \(email`, `\)size, \(rating`, `\)default, $widget |
自定义头像 |
markdown |
filter | \(text`, `\)widget |
过滤 Markdown |
autoP |
filter | \(text`, `\)widget |
过滤自动段落 |
content |
filter | \(content`, `\)widget |
过滤评论内容 |
contentEx |
filter | \(content`, `\)widget |
扩展评论内容 |
内容管理 Hook
Widget\Contents\Post\Edit / Page\Edit
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
write |
filter | \(post/\)page |
过滤写入数据 |
finishPublish |
call | \(post/\)page |
发布完成 |
finishSave |
call | \(post/\)page |
保存完成 |
mark |
call | \(post/\)page |
状态标记 |
finishMark |
call | \(post/\)page |
标记完成 |
delete |
call | \(post/\)page |
删除时 |
finishDelete |
call | \(post/\)page |
删除完成 |
Widget\Comments\Edit
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
mark |
call | $comment |
状态标记 |
delete |
call | $comment |
删除时 |
finishDelete |
call | $comment |
删除完成 |
edit |
filter | $comment |
过滤编辑数据 |
finishEdit |
call | $comment |
编辑完成 |
listComments |
filter | $options |
过滤列表选项 |
用户系统 Hook
Widget\User
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
register |
filter | $user |
过滤注册数据 |
finishRegister |
call | $user |
注册完成 |
logout |
call | 无 | 登出时 |
Widget\Login
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
login |
call | \(name`, `\)password, $remember |
登录尝试 |
hashValidate |
call+trigger | \(password`, `\)hash |
密码验证 |
loginSucceed |
call | $user |
登录成功 |
loginFail |
call | $user |
登录失败 |
simpleLoginSucceed |
call | $user |
简单登录成功 |
simpleLoginFail |
call | $name |
简单登录失败 |
文件上传 Hook
Widget\Upload
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
deleteHandle |
call+trigger | $content |
自定义删除处理 |
attachmentHandle |
call+trigger | $attachment |
自定义附件 URL |
attachmentDataHandle |
call+trigger | $content |
自定义附件数据 |
beforeModify |
call | $file |
修改前 |
modify |
call | $file |
修改时 |
modifyHandle |
call+trigger | $file |
自定义修改 |
beforeUpload |
call | $file |
上传前 |
upload |
call | $file |
上传时 |
uploadHandle |
call+trigger | $file |
自定义上传 |
评论反馈 Hook
Widget\Feedback
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
comment |
filter | $comment |
过滤评论数据 |
finishComment |
call | $comment |
评论完成 |
reply |
call | \(comment`, `\)parent |
回复时 |
cancelReply |
call | 无 | 取消回复 |
trackback |
filter | $trackback |
过滤引用 |
finishTrackback |
call | $trackback |
引用完成 |
pingback |
filter | $pingback |
过滤 Pingback |
finishPingback |
call | $pingback |
Pingback 完成 |
其他系统 Hook
Widget\Backup
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
export |
call | $backup |
备份导出 |
Widget\XmlRpc
| Hook 名称 | 类型 | 参数 | 说明 |
|---|---|---|---|
textFilter |
filter | $text |
过滤文本内容 |
Widget 动态扩展
动态属性
使用 ___ 前缀添加自定义属性:
// 注册
\Typecho\Plugin::factory('Widget\Archive')->___readTime = [Plugin::class, 'getReadTime'];
// 实现
public static function getReadTime($archive)
{
return ceil($archive->length / 300);
}
// 模板中使用
<?php echo $this->readTime; ?>动态方法
使用 call 前缀添加自定义方法:
// 注册
\Typecho\Plugin::factory('Widget\Archive')->callCustomMethod = [Plugin::class, 'handleMethod'];
// 实现
public static function handleMethod(\(archive, \)args)
{
echo 'Custom output';
}
// 模板中调用
<?php $this->customMethod(); ?>附录:常用代码示例
基本文章循环
<?php while ($this->next()): ?>
<article>
<h2><a href="<?php \(this->permalink(); ?>"><?php \)this->title(); ?></a></h2>
<p class="meta">
<?php $this->date('Y-m-d'); ?> ·
<?php $this->author(); ?> ·
<?php $this->commentsNum(); ?>
</p>
<div class="content">
<?php $this->content('阅读全文'); ?>
</div>
<p class="tags">
<?php $this->tags(' ', true, '无标签'); ?>
</p>
</article>
<?php endwhile; ?>评论列表
<?php \(comments = \)this->comments(); ?>
<?php while ($comments->next()): ?>
<li id="<?php $comments->theId(); ?>">
<div class="avatar">
<?php $comments->gravatar(40); ?>
</div>
<div class="comment-info">
<span class="author"><?php $comments->author(); ?></span>
<span class="time"><?php $comments->date(); ?></span>
</div>
<div class="comment-content">
<?php $comments->content(); ?>
</div>
</li>
<?php endwhile; ?>条件判断
<?php if ($this->is('index')): ?>
<!-- 首页内容 -->
<?php elseif ($this->is('post')): ?>
<!-- 文章页内容 -->
<?php elseif ($this->is('page')): ?>
<!-- 独立页内容 -->
<?php elseif ($this->is('category')): ?>
<!-- 分类页内容 -->
<?php else: ?>
<!-- 其他页面 -->
<?php endif; ?>获取自定义字段
<?php
\(thumb = \)this->fields->thumb;
if (!empty($thumb)) {
echo '<img src="' . \(thumb . '" alt="' . \)this->title . '" />';
}
?>版本说明
本文档基于 Typecho 1.3+ 版本整理,部分 API 在旧版本中可能不可用。
文档生成时间: 2026-02-03
适用版本: Typecho 1.3+
暂无评论
还没有评论,快来抢沙发吧!