总结一些Typecho有趣的调用语法站点名称<?php $this->options->title() ?>站点网址<?php $this->options ->siteUrl(); ?>站点说明<?php $this->options->description() ?>文章/页面的作者<?php $this->author(); ?>作者头像< ?php $this->author->gravatar('40') ?>上下篇调用代码<?php $this->thePrev(); ?> <?php $this->theNext(); ?>判断是否为首页,输出相关内容<?php if ($this->is('index')): ?> //是首页输出内容 <?php else: ?> //不是首页输出内容 <?php endif; ?>文章/页面评论数目<?php $this->commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>截取文章内容显示摘要(350 是字符数)<?php $this->excerpt(350, '.. .'); ?>调用自定义字段<?php $this->fields->fieldName ?>RSS 地址<?php $this->options->feedUrl(); ?>获取最新评论列表<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 $this->category(',', false); ?>获取文章时间归档<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 if($this->user->hasLogin()): ?> //登陆可见 <?php else: ?> //未登录和登陆均可见 <?php endif; ?>自动调用 img 字段内容,如果没有,去文章搜索第 1 个图片<?php if (array_key_exists('img',unserialize($this->___fields()))): ?><?php $this->fields->img(); ?><?php else: ?><?php preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches); $imgCount = count($matches[0]); if($imgCount >= 1){ $img = $matches[2][0]; echo <<<Html {$img} Html; } ?><?php endif; ?>文章内容替换七牛网址<?php echo $str = str_replace("your.com/usr/uploads","your.qiniu.com/usr/uploads",$this->content); ?>文章字数统计在 functions.php 中写入代码: function art_count ($cid){ $db=Typecho_Db::get (); $rs=$db->fetchRow ($db->select ('table.contents.text')->from ('table.contents')->where ('table.contents.cid=?',$cid)->order ('table.contents.cid',Typecho_Db::SORT_ASC)->limit (1)); echo mb_strlen($rs['text'], 'UTF-8'); } 在模板中调用: <?php echo art_count($this->cid); ?>自动调用第 1 个文章图片<?php preg_match_all("/\<img.*?src\=(\'|\")(.*?)(\'|\")[^>]*>/i", $this->content, $matches); $imgCount = count($matches[0]); if($imgCount >= 1){ $img = $matches[2][0]; echo <<<Html <p class="post-images"> <a href="{$this->permalink}" title="{$this->title}"> <img src="{$img}" alt="{$this->title}"> </a> </p> Html; } ?>边栏不显示博主评论<?php $this->widget('Widget_Comments_Recent','ignoreAuthor=true')->to($comments); ?>前台登录表单<form action="<?php $this->options->loginAction()?>" method="post" name="login" rold="form"> <input type="hidden" name="referer" value="<?php $this->options->siteUrl(); ?>"> <input type="text" name="name" autocomplete="username" placeholder="请输入用户名" required/> <input type="password" name="password" autocomplete="current-password" placeholder="请输入密码" required/> <button type="submit">登录</button> </form> 评论增加楼层显示<?php if($comments->levels == 0): ?> <?php if($comments->sequence == 1): ?>沙发 <?php elseif($comments->sequence == 2): ?>板凳 <?php elseif($comments->sequence == 3): ?>地毯 <?php else: ?> 第<?php $comments->sequence(); ?>楼<?php endif; ?> <?php endif; ?> 使用方法:放置在你的评论文件中评论列表循环处。根据文章访问量分等级function Viewlevel($cid){ $db = Typecho_Db::get(); $exist = $db->fetchRow($db->select('str_value')->from('table.fields')->where('cid = ?', $cid))['str_value']; //这里需要将 str_value 修改成你的阅读量数据库字段 if($exist<100){ echo '<span>新文</span>'; }elseif ($exist<300 && $exist>=100) { echo '<span>爽文</span>'; }elseif ($exist<1000 && $exist>=300) { echo '<span>荐文</span>'; }elseif ($exist<5000 && $exist>=1000) { echo '<span>热文</span>'; }elseif ($exist<10000 && $exist>=5000) { echo '<span>头条</span>'; }elseif ($exist<30000 && $exist>=10000) { echo '<span>爆文</span>'; }elseif ($exist>=30000) { echo '<span>神贴</span>'; }} 调用代码:<?php Viewlevel($this->cid); ?> 可以用在首页文章列表页显示,根据页面浏览量分为各种标签,或者也可以像我首页一样替换为图标等等实现那年今日功能function _getHistoryToday($created){ $date = date('m/d', $created); $time = time(); $db = Typecho_Db::get(); $prefix = $db->getPrefix(); $sql = "SELECT * FROM `{$prefix}contents` WHERE DATE_FORMAT(FROM_UNIXTIME(created), '%m/%d') = '{$date}' and created <= {$time} and created != {$created} and type = 'post' and status = 'publish' and (password is NULL or password = '') LIMIT 5"; $result = $db->query($sql); if($result instanceof Traversable) { foreach ($result as $item) { $item = Typecho_Widget::widget('Widget_Abstract_Contents')->push($item); $title = htmlspecialchars($item['title']); $permalink = $item['permalink']; echo "<li class='item'><a class='link' href='{$permalink}' title='{$title}'>{$title}</a></li>";}}} 文章内调用:<?php _getHistoryToday($this->created) ?> 全站内调用:<?php _getHistoryToday(time()) ?> 可以实现调用去年、前年或者很多年前当天发布的文章。全站数据调用<?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?> 文章总数:<?php $stat->publishedPostsNum() ?>篇 分类总数:<?php $stat->categoriesNum() ?>个 评论总数:<?php $stat->publishedCommentsNum() ?>条 页面总数:<?php $stat->publishedPagesNum() ?>页实现功能后台开关按钮function themeConfig($form){ $test = new Typecho_Widget_Helper_Form_Element_Select('test',array(0=>'不开启',1=>'开启'),0,'测试功能','开启网站测试功能'); $form->addInput($test);} 前台放入以下代码: <?php if($this -> options -> test == '1'): ?> 这里可以放执行的代码、样式等内容 <?php endif; ?> 有些不常用的代码或者效果,可以自己加个开关在后台控制,免去每次加了再删除再添加的尴尬。