分享一些 Typecho 主題常用的函數
57,941 1
以前還在使用 Typecho 的時候,就經常自己瞎折騰主題外掛,然後就要經常去 docs.typecho.org 查看關於主題外掛的常用函數。
為了方便更多的開發者和初學者能更好、更快捷地學習製作 Typecho 主題,我把這些經常會用到的函數整理了一下,發出來給小夥伴們使用!

網站名稱
<?php $this->options->title() ?>
網站網址
<?php $this->options->siteUrl(); ?>
完整路徑標題
<?php $this->archiveTitle(' » ', '', ' | '); ?><?php $this->options->title(); ?>
網站說明
<?php $this->options->description() ?>
範本資料夾位址
<?php $this->options->themeUrl(); ?>
匯入範本資料夾內的 PHP 檔案
<?php $this->need('.php'); ?>
文章或頁面的作者
<?php $this->author(); ?>
作者頭像
<?php $this->author->gravatar('40') ?>
此處輸出的是完整的 img 標籤,40 是頭像的寬度和高度。
該文作者全部文章列表連結
<?php $this->author->permalink (); ?>
該文作者個人首頁連結
<?php $this->author->url(); ?>
該文作者的電子郵件地址
<?php $this->author->mail(); ?>
上一篇與下一篇呼叫程式碼
<?php $this->thePrev(); ?>
<?php $this->theNext(); ?>
判斷是否為首頁,輸出相關內容
<?php if ($this->is('index')): ?>
// 首页输出内容
<?php else: ?>
// 不是首页输出内容
<?php endif; ?>
文章或頁面,評論數量
<?php $this->commentsNum('No Comments', '1 Comment', '%d Comments'); ?>
擷取部分文章(首頁每篇文章顯示摘要)
<?php $this->excerpt(200, '...'); ?>
200 是擷取的字數。
呼叫自訂欄位
<?php $this->fields->fieldName ?>
RSS 位址
取得最新 post
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
純文字分類名稱,不帶連結
<?php $this->category(',', false); ?>
取得文章分類列表
<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
<ul>
<?php
$this->widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')
->parse('<li><a href="{permalink}" title="{title}">{title}</a></li>'); ?>
</ul>
取得最新评论列表
<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>
首頁取得最新文章限制條數
<?php while ($this->next()): ?>
<?php if ($this->sequence <= 3): ?>
html
<?php endif; ?>
<?php endwhile; ?>
取得最新评论列表第二個版本,只顯示訪客評論不顯示博主(也就是作者或者說自己發的評論)
<?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; ?>
取得文章時間歸檔
<ul>
<?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')
->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
</ul>
取得標籤集合,也就是標籤雲
<?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; ?>
呼叫該文相關文章列表
<?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 區域的程式版本和範本名稱
<?php $this->header("generator=&template="); ?>
取得讀者牆
<?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;
?>
登入與未登入使用者顯示不同內容
<?php if($this->user->hasLogin()): ?>
登入可見
<?php else: ?>
未登入和登入均可見
<?php endif; ?>
評論
(1)重返独立博客,写板子,查文章,谢谢分享。