---
title: "分享一些 Typecho 主题开发过程中常用的钩子函数 - 一纸忘忧"
description: "以前还在用 Typecho 的时候，就经常自己瞎折腾主题插件，然后就要经常去 docs.typecho.org 查看关于主题插件的常用函数。"
canonical_url: "https://www.ikxin.com/article/typecho-theme-functions"
last_updated: "2026-09-16T09:59:27.259Z"
locale: zh-cn
meta:
  "og:description": "以前还在用 Typecho 的时候，就经常自己瞎折腾主题插件，然后就要经常去 docs.typecho.org 查看关于主题插件的常用函数。"
  "og:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aKY5byA5Y-R6L-H56iL5Lit5bi455So55qE6ZKp5a2Q5Ye95pWwIC0g5LiA57q45b-Y5b-n%2Cc_Article%2Ctitle_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aKY5byA5Y-R6L-H56iL5Lit5bi455So55qE6ZKp5a2Q5Ye95pWw%2Cdescription_~5Lul5YmN6L-Y5Zyo55SoIFR5cGVjaG8g55qE5pe25YCZ77yM5bCx57uP5bi46Ieq5bex556O5oqY6IW-5Li76aKY5o-S5Lu277yM54S25ZCO5bCx6KaB57uP5bi45Y67IGRvY3MudHlwZWNoby5vcmcg5p-l55yL5YWz5LqO5Li76aKY5o-S5Lu255qE5bi455So5Ye95pWw44CC%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3R5cGVjaG8tdGhlbWUtZnVuY3Rpb25zIg%2Cs_yHJhfAPthd1O-i84.png"
  "og:image:alt": "分享一些 Typecho 主题开发过程中常用的钩子函数 - 一纸忘忧"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "分享一些 Typecho 主题开发过程中常用的钩子函数"
  "twitter:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aKY5byA5Y-R6L-H56iL5Lit5bi455So55qE6ZKp5a2Q5Ye95pWwIC0g5LiA57q45b-Y5b-n%2Cc_Article%2Ctitle_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aKY5byA5Y-R6L-H56iL5Lit5bi455So55qE6ZKp5a2Q5Ye95pWw%2Cdescription_~5Lul5YmN6L-Y5Zyo55SoIFR5cGVjaG8g55qE5pe25YCZ77yM5bCx57uP5bi46Ieq5bex556O5oqY6IW-5Li76aKY5o-S5Lu277yM54S25ZCO5bCx6KaB57uP5bi45Y67IGRvY3MudHlwZWNoby5vcmcg5p-l55yL5YWz5LqO5Li76aKY5o-S5Lu255qE5bi455So5Ye95pWw44CC%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3R5cGVjaG8tdGhlbWUtZnVuY3Rpb25zIg%2Cs_yHJhfAPthd1O-i84.png"
  "twitter:image:alt": "分享一些 Typecho 主题开发过程中常用的钩子函数 - 一纸忘忧"
---

> Index of every page on this site: [llms.txt](https://www.ikxin.com/llms.txt)

# **分享一些 Typecho 主题开发过程中常用的钩子函数**

2017.07.31 58,119  1

以前还在用 Typecho 的时候，就经常自己瞎折腾主题插件，然后就要经常去 [docs.typecho.org](https://docs.typecho.org) 查看关于主题插件的常用函数。

为了方便更多的开发者和初学者能更好更快捷地学习制作 Typecho 主题，我把这些经常会用到的函数整理了一下，发出来给小伙伴们使用！

![图片](https://www.ikxin.com/_ipx/animated_true&w_1734&f_webp/https://img.8b5.cn/2019/06/25/5d11adf707b49.jpg)

### 站点名称

 Plain Text

```
<?php $this->options->title() ?>
```

### 站点网址

 Plain Text

```
<?php $this->options->siteUrl(); ?>
```

### 完整路径标题

 Plain Text

```
<?php $this->archiveTitle(' &raquo; ', '', ' | '); ?><?php $this->options->title(); ?>
```

### 站点说明

 Plain Text

```
<?php $this->options->description() ?>
```

### 模板文件夹地址

 Plain Text

```
<?php $this->options->themeUrl(); ?>
```

### 导入模板文件夹内的 PHP 文件

 Plain Text

```
<?php $this->need('.php'); ?>
```

### 文章或者页面的作者

 Plain Text

```
<?php $this->author(); ?>
```

### 作者头像

 Plain Text

```
<?php $this->author->gravatar('40') ?>
```

此处输出的是完整的 `img` 标签，`40` 是头像的宽度和高度。

### 该文作者全部文章列表链接

 Plain Text

```
<?php $this->author->permalink (); ?>
```

### 该文作者个人主页链接

 Plain Text

```
<?php $this->author->url(); ?>
```

### 该文作者的邮箱地址

 Plain Text

```
<?php $this->author->mail(); ?>
```

### 上一篇与下一篇调用代码

 Plain Text

```
<?php $this->thePrev(); ?><?php $this->theNext(); ?>
```

### 判断是否为首页，输出相关内容

 Plain Text

```
<?php if ($this->is('index')): ?>
```

`// 首页输出内容`

 Plain Text

```
<?php else: ?>
```

`// 不是首页输出内容`

 Plain Text

```
<?php endif; ?>
```

### 文章或页面，评论数目

 Plain Text

```
<?php $this->commentsNum('No Comments', '1 Comment', '%d Comments'); ?>
```

### 截取部分文章（首页每篇文章显示摘要）

 Plain Text

```
<?php $this->excerpt(200, '...'); ?>
```

`200` 是截取的字数。

### 调用自定义字段

 Plain Text

```
<?php $this->fields->fieldName ?>
```

### RSS 地址

### 获取最新 post

 Plain Text

```
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
```

### 纯文字分类名称，不带链接

 Plain Text

```
<?php $this->category(',', false); ?>
```

### 获取文章分类列表

 Plain Text

```
<ul><?php $this->widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')->parse('<li><a href="{permalink}" title="{title}">{title}</a></li>'); ?></ul>
```

### 获取某分类 post

 Plain Text

```
<ul><?php $this->widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')->parse('<li><a href="{permalink}" title="{title}">{title}</a></li>'); ?></ul>
```

### 获取最新评论列表

 Plain Text

```
<ul>    <?php $this->widget('Widget_Comments_Recent')->to($comments); ?>    <?php while($comments->next()): ?>        <li><a href="<?php $comments->permalink(); ?>">        <?php $comments->author(false); ?></a>:        <?php $comments->excerpt(50, '...'); ?></li>    <?php endwhile; ?></ul>
```

### 首页获取最新文章限制条数

 Plain Text

```
<?php while ($this->next()): ?><?php if ($this->sequence <= 3): ?>html<?php endif; ?><?php endwhile; ?>
```

### 获取最新评论列表第二个版本，只显示访客评论不显示博主（也就是作者或者说自己发的评论）

 Plain Text

```
<?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>    <?php while($comments->next()): ?>    <li><a href="<?php $comments->permalink(); ?>">    <?php $comments->author(false); ?></a>:    <?php $comments->excerpt(50, '...'); ?></li><?php endwhile; ?>
```

### 获取文章时间归档

 Plain Text

```
<ul>    <?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')                ->parse('<li><a href="{permalink}">{date}</a></li>'); ?></ul>
```

### 获取标签集合，也就是标签云

 Plain Text

```
<?php $this->widget('Widget_Metas_Tag_Cloud', 'ignoreZeroCount=1&limit=28')->to($tags); ?><?php while($tags->next()): ?><a href="<?php $tags->permalink(); ?>" class="size-<?php $tags->split(5, 10, 20, 30); ?>"><?php $tags->name(); ?></a><?php endwhile; ?>
```

### 调用该文相关文章列表

 Plain Text

```
<?php $this->related(5)->to($relatedPosts); ?>    <?php if ($relatedPosts->have()): ?>    <?php while ($relatedPosts->next()): ?>        <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>    <?php endwhile; ?>    <?php else : ?>        <li>无相关文章</li>    <?php endif; ?>
```

### 隐藏 head 区域的程序版本和模板名称

 Plain Text

```
<?php $this->header("generator=&template="); ?>
```

### 获取读者墙

 Plain Text

```
<?php$period = time() - 999592000; // 时段: 30 天, 单位: 秒$counts = Typecho_Db::get()->fetchAll(Typecho_Db::get()->select('COUNT(author) AS cnt','author', 'url', 'mail')->from('table.comments')->where('created > ?', $period )->where('status = ?', 'approved')->where('type = ?', 'comment')->where('authorId = ?', '0')->group('author')->order('cnt', Typecho_Db::SORT_DESC)->limit(25));$mostactive = '';$avatar_path = 'http://www.gravatar.com/avatar/';foreach ($counts as $count) {  $avatar = $avatar_path . md5(strtolower($count['mail'])) . '.jpg';  $c_url = $count['url']; if ( !$c_url ) $c_url = Helper::options()->siteUrl;  $mostactive .= "<a href='" . $c_url . "' title='" . $count['author'] . " (参与" . $count['cnt'] . "次互动)' target='_blank'><img src='" . $avatar . "' alt='" . $count['author'] . "的头像' class='avatar' width='32' height='32' /></a>\n";}echo $mostactive;?>
```

### 登录与未登录用户展示不同内容

 Plain Text

```
<?php if($this->user->hasLogin()): ?>
```

登录可见

 Plain Text

```
<?php else: ?>
```

未登录和登录均可见

 Plain Text

```
<?php endif; ?>
```

## **评论**

(1)

1. [**zizdog**](http://zizdog.com)2020/12/05 16:34 Firefox 83 Windows 10

   重返独立博客，写板子，查文章，谢谢分享。

## **发布评论**

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://www.ikxin.com/#website","@type":"WebSite","description":"一纸忘忧的个人网站，关于技术探索与日常生活，记录 Web 开发、服务端与 DevOps 实践，分享软件工具、数码体验，以及代码之外的见闻与思考。","inLanguage":"zh-CN","name":"一纸忘忧","url":"https://www.ikxin.com/","workTranslation":[{"@id":"https://www.ikxin.com/zh-tw#website"},{"@id":"https://www.ikxin.com/ko#website"},{"@id":"https://www.ikxin.com/en#website"},{"@id":"https://www.ikxin.com/ja#website"}]},{"@id":"https://www.ikxin.com/article/typecho-theme-functions#webpage","@type":"WebPage","description":"以前还在用 Typecho 的时候，就经常自己瞎折腾主题插件，然后就要经常去 docs.typecho.org 查看关于主题插件的常用函数。","name":"分享一些 Typecho 主题开发过程中常用的钩子函数","url":"https://www.ikxin.com/article/typecho-theme-functions","isPartOf":{"@id":"https://www.ikxin.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://www.ikxin.com/article/typecho-theme-functions"]}]}]}
```

## Sitemap

See the full [sitemap](https://www.ikxin.com/sitemap.md) for all pages.
