Sharing Some Commonly Used Functions for Typecho Themes

57,941 1

When I was still using Typecho, I often tinkered with themes and plugins myself, and therefore frequently had to visit docs.typecho.org to look up commonly used functions for themes and plugins.

To make it easier for more developers and beginners to learn how to create Typecho themes more effectively and quickly, I organized some of the functions that are often used and am sharing them here for everyone to use!

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() ?>

Template Folder URL

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

Import a PHP File from the Template 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.

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

Link to the Article Author's Personal Homepage

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

Email Address of the Article 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 URL

Get the Latest Posts

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 from a Specific 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 Displayed on the Homepage

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

Get the Latest Comments List, Version Two: Display Visitor Comments Only, Excluding 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 the 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; ?>

Call the List of Related Articles for This Article

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 Template 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