Typecho是一款简单易用的博客系统,它有很多功能,其中之一就是语法、函数的调用。这意味着你可以用简单的语句来控制你的博客显示什么内容,比如文章、页面、标签、分类和评论等。这样你就可以轻松地管理和展示你的博客内容,让你的博客更有个性化。

站点网址

<?php $this->options->title() ?><!--方法1-->
<?php $this->options ->siteUrl(); ?><!--方法2-->

完整路径标题

<?php $this->archiveTitle(' &raquo; ', < span class="string">'', ' | '); ?>
<?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') ?>

该文作者全部文章列表链接

<?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(350, '...'); ?> 

调用自定义字段

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

RSS 地址

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

获取最新 post

<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?> 

纯文字分类名称,不带链接

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

获取当前文章所属分类,包含链接

<?php if ($this->is('post')): ?>
    <span><?php $this->category(' '); ?></span>
<?php endif; ?> 

获取当前文章所属分类,包含链接

<?php if ($this->is('post')): ?>
    <span><?php $this->category(' '); ?></span>
<?php endif; ?> 

获取文章分类列表

<ul>
<?php $this->widget('Widget_Metas_Category_List')
                ->parse('<li><a href="{permalink}">{name}</a> ({count})</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()): ?>    //这句也可以写成 if (count($relatedPosts->stack))
    <ul><?php while ($relatedPosts->next()): ?>
        <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
    <?php endwhile; ?></ul>
    <?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; ?> 

导航页面列表调用隐藏特定的页面

<ul>
<li<?php if($this->is('index')): ?> class="current"<?php endif; ?>><a href="<?php $this->options->siteUrl(); ?>">主页</a></li>
<?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
    <?php while($pages->next()): ?>
    <?php if (($pages->slug != 'album') && ($pages->slug != 'search')): ?>
    <li<?php if($this->is('page', $pages->slug)): ?> class="current"<?php endif; ?>><a href="<?php $pages->permalink(); ?>" title="<?php $pages->title(); ?>"><?php $pages->title(); ?></a></li>
    <?php endif; ?>
    <?php endwhile; ?>
</ul>

Type­cho 归档页面

<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
    $year=0; $mon=0; $i=0; $j=0;
    $output = '<div id="archives">';
    while($archives->next()):
        $year_tmp = date('Y',$archives->created);
        $mon_tmp = date('m',$archives->created);
        $y=$year; $m=$mon;
        if ($mon != $mon_tmp && $mon > 0) $output .= '</ul></li>';
        if ($year != $year_tmp && $year > 0) $output .= '</ul>';
        if ($year != $year_tmp) {
            $year = $year_tmp;
            $output .= '<h3 class="al_year">'. $year .' 年</h3><ul class="al_mon_list">'; //输出年份
        }
        if ($mon != $mon_tmp) {
            $mon = $mon_tmp;
            $output .= '<li><span class="al_mon">'. $mon .' 月</span><ul class="al_post_list">'; //输出月份
        }
        $output .= '<li>'.date('d日: ',$archives->created).'<a href="'.$archives->permalink .'">'. $archives->title .'</a> <em>('. $archives->commentsNum.')</em></li>'; //输出文章日期和标题
    endwhile;
    $output .= '</ul></li></ul></div>';
    echo $output;
?>

获取当前文章页缩略图

<?php $this->attachments(1)->attachment->url(); ?>

根据页面类型显示内容,判断是文章页则显示内容

<?php if ($this->is('post')): ?>
想要显示的内容1
<?php endif; ?>

根据页面类型显示内容,判断是页面则显示内容

<?php if ($this->is('page', 'about')): ?>
想要显示的内容2
<?php endif; ?>

Title增加副标题

<?php if ($this->is('index')): ?> - 副标题<?php endif; ?>

按分类输出cms

<div class="row">
<?php $this->widget('Widget_Metas_Category_List')->to($categories); ?>
<?php while ($categories->next()): ?>
<?php if(count($categories->children) === 0): ?>
<?php $this->widget('Widget_Archive@category-' . $categories->mid, 'pageSize=5&type=category', 'mid=' . $categories->mid)->to($posts); ?>
<div class="col-lg-6">
  <div class="panel panel-default">
      <div class="panel-heading">
          <i class="glyphicon glyphicon-th"></i> <a href="<?php $categories->permalink(); ?>" class="guidang" id="posts-list-<?php $categories->slug(); ?>"><?php $categories->name(); ?></a>
        <a href="<?php $categories->permalink(); ?>" class="pull-right" id="posts-list-<?php $categories->slug(); ?>"><i class="glyphicon glyphicon-option-horizontal"></i></a>
      </div>
      <div class="panel-body">
        <?php while ($posts->next()): ?>
        <p class="overflow">
          <a href="<?php $posts->permalink(); ?>" title="<?php $posts->title(40); ?>"><i class="glyphicon glyphicon-chevron-right"></i> <?php $posts->title(40); ?></a>
        </p>
        <?php endwhile; ?>
      </div>
  </div>
</div>
<?php else: ?>
<?php endif; ?>
<?php endwhile; ?>
</div>

相关文章

<?php $this->related(5)->to($relatedPosts); ?>
    <ul>
    <?php while ($relatedPosts->next()): ?>
    <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
    <?php endwhile; ?>
</ul>

侧边栏热门标签

<div class="widget">
<h3><?php _e('热门标签'); ?></h3>
<ul class="cate">
<?php $this->widget('Widget_Metas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => 20))->to($tags); ?>
<?php while($tags->next()): ?>
<li><a rel="tag" href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a></li>
<?php endwhile; ?>
<div class="clear"></div>
</ul>
</div>

侧边栏输出所有标签

<div class="widget">
<h3><?php _e('所有标签'); ?></h3>
<ul class="cate">
<?php $this->widget('Widget_Metas_Tag_Cloud')->to($tags); ?>
<?php while($tags->next()): ?>
<li><a rel="tag" href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a></li>
<?php endwhile; ?>
<div class="clear"></div>
</ul>
</div>

当前文章的标签

<?php $this->tags(',', true, 'none'); ?>
(',', true, 'none') 第一个单引号间的逗号代表标签与标签的间隔用逗号隔开,true 是标签以超链接形式输出 false 则只输出文字,none 为该文章没有标签时显示的提示信息可为空。

如果想给每个标签套上 div 或者 span 什么的就需要这要做

<div><?php $this->tags('</div><div>', true, 'none'); ?></div>

判断当前标签并自定义内容

<?php if( in_array('abc', $this->tags) ): ?>
文章含有abc标签就会显示这里
<?php endif; ?>

判断文章是否存在标签,如果存在输出标签,如果不存在输出该文章分类

<?php if(  count($this->tags) == 0 ): ?> //此处的count也可改为size
<?php $this->category(',', true, 'none'); ?> //文章分类
<?php else: ?>
<?php $this->tags(',', true, 'none'); ?> //文章标签
<?php endif; ?>

原文地址