Typecho テーマ開発でよく使われるフック関数をいくつか紹介

57,998 1

Typecho を使っていた頃は、テーマやプラグインを自分でいろいろ試すことが多く、そのたびに docs.typecho.org でテーマやプラグインに関するよく使う関数を確認していました。

より多くの開発者や初心者が、Typecho テーマの作成をより便利かつスムーズに学べるように、よく使う関数を整理してまとめました。ぜひ皆さんでご利用ください!

画像

サイト名

Plain Text
<?php $this->options->title() ?>

サイト URL

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; ?>

最新コメント一覧を取得する第 2 版。訪問者のコメントのみを表示し、ブログ管理者(つまり著者または自分自身)のコメントは表示しない

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. Firefox 83 Windows 10

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

コメントを投稿

憂いを忘れる一枚

「憂いを忘れる一枚」は、技術探究と日々の暮らしをテーマにした個人サイトです。Web 開発、バックエンド、DevOps の実践を記録し、ソフトウェアツールやデジタル体験、コードの外で得た見聞と思索を共有します。

© 2026 憂いを忘れる一枚. 無断転載を禁じます.