Typecho $this Syntax

0, Preface

When I first came into contact with Typecho, I saw a lot of words like $this->, but I didn’t know what it meant. I checked the documentation and found out that it is a special syntax, and some parameters of the current page can be called.

1, syntax list

Site name

PHP
1
<?php $this->options->title() ?>

site url

PHP
1
<?php $this->options->siteUrl(); ?>

site introduction

PHP
1
<?php $this->options->description() ?>

Author of the article/page

PHP
1
<?php $this->author(); ?>

Author Avatar

PHP
1
<?php $this->author->gravatar('40') ?>

Among them, 40 is the avatar size, which can be modified.

call up and down

PHP
1
<?php $this->thePrev(); ?> <?php $this->theNext(); ?>

Determine whether it is the home page and output relevant content

PHP
1
2
3
4
5
<?php if ($this->is('index')): ?>
// is the output content of the home page
<?php else: ?>
//not the home page output content
<?php endif; ?>

Number of Article/Page Comments

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

Intercept the content of the article and display the summary

PHP
1
<?php $this->excerpt(350, '.. .'); ?>

Among them, 350 is the number of words to intercept the summary.

call custom fields

PHP
1
<?php $this->fields->fieldName ?>

filedName is the field name, for example the language field can be written:

PHP
1
<?php $this->fields->language ?>

RSS address

PHP
1
<?php $this->options->feedUrl(); ?>

Get the latest review list

PHP
1
2
3
4
5
6
<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>

Category Name

PHP
1
<?php $this->category(',', false); ?>

Get article time archive

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

Get the label collection

PHP
1
2
3
4
<?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; ?>

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

PHP
1
2
3
4
5
<?php if($this->user->hasLogin()): ?>
// login visible
<?php else: ?>
// Both logged in and logged in are visible
<?php endif; ?>
Author
Hsukqi Lee
Posted on

2023-05-21

Edited on

2023-05-21

Licensed under

CC BY-NC-ND 4.0

Comments

Name
Mail
Site
None yet