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

최신 댓글 목록 가져오기 두 번째 버전: 방문자 댓글만 표시하고 블로거(즉, 작성자 또는 본인이 작성한 댓글)는 표시하지 않기

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

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

댓글 작성