Sharing Some Commonly Used Hook Functions in Typecho Theme Development

58,000 1

Back when I was still using Typecho, I often tinkered with themes and plugins myself, so I frequently had to visit docs.typecho.org to check the commonly used functions for themes and plugins.

To make it easier for more developers and beginners to learn how to create Typecho themes better and more quickly, I organized these frequently used functions and am sharing them with everyone!

Image

Site name

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

Site URL

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

Full-path title

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

Site description

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

Theme folder address

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

Import a PHP file from the theme folder

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

Author of an article or page

Plain Text
<?php $this->author(); ?>

Author avatar

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

This outputs a complete img tag. 40 is the width and height of the avatar.

Link to all articles by the author of the article

Plain Text
<?php $this->author->permalink (); ?>
Plain Text
<?php $this->author->url(); ?>

Email address of the article's author

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

Code for calling the previous and next articles

Plain Text
<?php $this->thePrev(); ?><?php $this->theNext(); ?>
Plain Text
<?php if ($this->is('index')): ?>

// 首页输出内容

Plain Text
<?php else: ?>

// 不是首页输出内容

Plain Text
<?php endif; ?>

Number of comments on an article or page

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

Extract part of an article (display an excerpt for each article on the homepage)

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

200 is the number of characters to extract.

Call a custom field

Plain Text
<?php $this->fields->fieldName ?>

RSS address

Get the latest 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); ?>

Get the article category list

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>

Get posts in a category

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>

Get the latest comments list

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>

Limit the number of latest articles retrieved on the homepage

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

Get the latest comments list, second version: show only visitor comments and not comments by the blogger (that is, the author or yourself)

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

Get the article date archive

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

Get the tag collection, also known as a tag cloud

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

Hide the program version and theme name in the head section

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

Get the reader wall

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

Display different content for logged-in and logged-out users

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

Visible to logged-in users

Plain Text
<?php else: ?>

Visible to both logged-out and logged-in users

Plain Text
<?php endif; ?>

Comments

(1)
  1. Firefox 83 Windows 10

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

Leave a comment