---
title: "分享一些 Typecho 主題開發過程中常用的鉤子函數 - 一紙忘憂"
description: "以前還在用 Typecho 的時候，就經常自己瞎折騰主題外掛，然後就要經常去 docs.typecho.org 查看關於主題外掛的常用函數。"
canonical_url: "https://www.ikxin.com/zh-tw/article/typecho-theme-functions"
last_updated: "2026-09-16T09:59:27.259Z"
locale: zh-tw
meta:
  "og:description": "以前還在用 Typecho 的時候，就經常自己瞎折騰主題外掛，然後就要經常去 docs.typecho.org 查看關於主題外掛的常用函數。"
  "og:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aGM6ZaL55m86YGO56iL5Lit5bi455So55qE6Ymk5a2Q5Ye95pW4IC0g5LiA57SZ5b-Y5oaC%2Cc_Article%2Ctitle_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aGM6ZaL55m86YGO56iL5Lit5bi455So55qE6Ymk5a2Q5Ye95pW4%2Cdescription_~5Lul5YmN6YKE5Zyo55SoIFR5cGVjaG8g55qE5pmC5YCZ77yM5bCx57aT5bi46Ieq5bex556O5oqY6aiw5Li76aGM5aSW5o6b77yM54S25b6M5bCx6KaB57aT5bi45Y67IGRvY3MudHlwZWNoby5vcmcg5p-l55yL6Zec5pa85Li76aGM5aSW5o6b55qE5bi455So5Ye95pW444CC%2CsiteName_~5LiA57SZ5b-Y5oaC%2CsiteHost_www.ikxin.com%2Cp_Ii96aC10dy9hcnRpY2xlL3R5cGVjaG8tdGhlbWUtZnVuY3Rpb25zIg%2Cs_yaPVv95XsqTQq5kW.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_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aGM6ZaL55m86YGO56iL5Lit5bi455So55qE6Ymk5a2Q5Ye95pW4IC0g5LiA57SZ5b-Y5oaC%2Cc_Article%2Ctitle_~5YiG5Lqr5LiA5LqbIFR5cGVjaG8g5Li76aGM6ZaL55m86YGO56iL5Lit5bi455So55qE6Ymk5a2Q5Ye95pW4%2Cdescription_~5Lul5YmN6YKE5Zyo55SoIFR5cGVjaG8g55qE5pmC5YCZ77yM5bCx57aT5bi46Ieq5bex556O5oqY6aiw5Li76aGM5aSW5o6b77yM54S25b6M5bCx6KaB57aT5bi45Y67IGRvY3MudHlwZWNoby5vcmcg5p-l55yL6Zec5pa85Li76aGM5aSW5o6b55qE5bi455So5Ye95pW444CC%2CsiteName_~5LiA57SZ5b-Y5oaC%2CsiteHost_www.ikxin.com%2Cp_Ii96aC10dy9hcnRpY2xlL3R5cGVjaG8tdGhlbWUtZnVuY3Rpb25zIg%2Cs_yaPVv95XsqTQq5kW.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,128  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/zh-tw#website","@type":"WebSite","description":"一紙忘憂的個人網站，關於技術探索與日常生活，記錄 Web 開發、後端與 DevOps 實踐，分享軟體工具、數位體驗，以及程式碼之外的見聞與思考。","inLanguage":"zh-TW","name":"一紙忘憂","url":"https://www.ikxin.com/zh-tw","translationOfWork":{"@id":"https://www.ikxin.com/#website"}},{"@id":"https://www.ikxin.com/zh-tw/article/typecho-theme-functions#webpage","@type":"WebPage","description":"以前還在用 Typecho 的時候，就經常自己瞎折騰主題外掛，然後就要經常去 docs.typecho.org 查看關於主題外掛的常用函數。","name":"分享一些 Typecho 主題開發過程中常用的鉤子函數","url":"https://www.ikxin.com/zh-tw/article/typecho-theme-functions","isPartOf":{"@id":"https://www.ikxin.com/zh-tw#website"},"potentialAction":[{"@type":"ReadAction","target":["https://www.ikxin.com/zh-tw/article/typecho-theme-functions"]}]}]}
```

## Sitemap

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