摘要: 下面是相关的函数 1、站点名称 <?php $this->options->title() ?> 2、站点网址 <?php $this->options ->siteUrl(); ?> 3、完整路径标题如分享几个Typecho中常用的调用函数 <?php $this->archiveTitle('...
1、站点名称
<?php $this->options->title() ?>
2、站点网址
<?php $this->options ->siteUrl(); ?>
3、完整路径标题如分享几个Typecho中常用的调用函数
<?php $this->archiveTitle(' » ', < span>'', ' | '); ?><?php $this ->options->title(); ?>
4、站点说明
<?php $this->options->description() ?>
5、模板文件夹地址
<?php $this->options->themeUrl(); ?>
6、导入模板文件夹内的php文件
<?php $this< /span>->need('.php'); ?>
7、文章或者页面的作者
<?php $this->author(); ?>
8、作者头像
< ?php $this->author->gravatar('40') ?>
此处输出的完整的img标签,40是头像的宽和高。
9、该文作者全部文章列表链接
<?php $this->author->permalink (); ?>
10、该文作者个人主页链接
<?php $this->author->url(); ?>
11 、该文作者的邮箱地址
<?php $this->author->mail(); ?>
12、上一篇与下一篇调用代码
<?php $this->thePrev(); ?> <?php $this->theNext(); ?>
13、判断是否为首页,输出相关内容
<?php if ($this->is('index')): ?>//首页输出内容
<?php else: ?>
//不是首页输出内容
< span><?php endif; ?>
14、文章或页面,评论数目
<?php $this</span><span>-</span><span>></span><span>commentsNum</span><span>(</span><span>'No Comments'</span><span>,</span> <span>'1 Comment'</span> <span>,</span> <span>'%d Comments'</span><span>)</span><span>;</span> <span>?></span></span></code></pre><p>14、文章或页面,评论数目</p><pre><code><span><span><?php</span> <span>$this->commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>
15、截取部份文章(首页每篇文章显示摘要),350是字数
<?php $this</span><span>-</span><span>></span><span>excerpt</span><span>(</span><span>350</span><span>,</span> <span>'.. .'</span><span>)</span><span>;</span> <span>?></span></span></code></pre><p>16、调用自定义字段(官方文档坑爹,竟然没有,博主自己摸索出来的)</p><pre><code><span><span><?php</span> <span>$this->fields->fieldName ?>
17、RSS地址
<?php $this</span><span>-</span><span>></span><span>options</span><span>-</span><span>></span><span>feedUrl</span><span>(</span><span>)</span><span>;</span> <span>?></span></span></code></pre><p>18、获取最新post</p><pre><code><span><span><?php</span> <span>$this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>
19、纯文字分类名称,不带链接
<?php $this->category(',', false); ?>
20、获取文章分类列表
<ul>
<?php $this->widget('Widget_Metas_Category_List') <span>-</span><span>></span><span>parse</span><span>(</span>'<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span>{permalink}<span>"</span></span><span>></span></span></span><span>{</span>name<span>}</span><span><span><span><span></</span>a</span><span>></span></span></span> <span>(</span><span>{</span>count<span>}</span><span>)</span><span><span><span><span></</span>li</span><span>></span></span></span>'<span>)</span><span>;</span> <span>?></span></span>
</ul>
21、获取某分类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>
22、获取最新评论列表
<ul><span><span><?php</span> <span>$this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Comments_Recent'</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$comments</span><span>)</span><span>;</span> <span>?></span></span>
<span><span><?php</span> <span>while</span><span>(</span><span>$comments</span><span>-</span><span>></span><span>next</span><span>(</span><span>)</span><span>)</span><span>:</span> <span>?></span></span>
<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>permalink</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span><span>></span></span></span><span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>author</span><span>(</span><span>false</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>a</span><span>></span></span></span><span>:</span> <span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>excerpt</span><span>(</span><span>50</span><span>,</span> <span>'...'</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>li</span><span>></span></span></span>
<span><span><?php</span> <span>endwhile</span><span>;</span> <span>?></span></span>
</ul>
23、首页获取 最新文章 代码限制条数
<?php while ($this->next()): ?>
<?php if ($this->sequence <= 3): ?>
html
<?php endif; ?>
<?php endwhile; ?>
24、获取最新评论列表第二个版本,只显示访客评论不显示博主也就是作者或者说自己发的评论
<?php $this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Comments_Recent'</span><span>,</span><span>'ignoreAuthor=true'</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$comments); ?><span><span><?php</span> <span>while</span><span>(</span><span>$comments</span><span>-</span><span>></span><span>next</span><span>(</span><span>)</span><span>)</span><span>:</span> <span>?></span></span>
<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>permalink</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span><span>></span></span></span><span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>author</span><span>(</span><span>false</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>a</span><span>></span></span></span><span>:</span> <span><span><?php</span> <span>$comments</span><span>-</span><span>></span><span>excerpt</span><span>(</span><span>50</span><span>,</span> <span>'...'</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>li</span><span>></span></span></span>
<?phpendwhile;?>
25、获取文章时间归档
<ul><span><span><?php</span> <span>$this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Contents_Post_Date'</span><span>,</span> <span>'type=month&format=F Y'</span><span>)</span>
<span>-</span><span>></span><span>parse</span><span>(</span>'<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span>{permalink}<span>"</span></span><span>></span></span></span><span>{</span>date<span>}</span><span><span><span><span></</span>a</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span>'<span>)</span><span>;</span> <span>?></span></span>
</ul>
26、获取标签集合,也就是标签云
<?php $this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Metas_Tag_Cloud'</span><span>,</span> <span>'ignoreZeroCount=1&limit=28'</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$tags); ?>
<?php while($tags->next()): ?>
<a href="<?php $tags</span><span>-</span><span>></span><span>permalink</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span> <span>class</span><span><span>=</span><span>"</span>size-<span><span><?php</span> <span>$tags->split(5, 10, 20, 30); ?>"><?php $tags->name(); ?></a>
<?php endwhile; ?>
27、调用该文相关文章列表
<?php $this</span><span>-</span><span>></span><span>related</span><span>(</span><span>5</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$relatedPosts); ?><span><span><?php</span> <span>if</span> <span>(</span><span>$relatedPosts</span><span>-</span><span>></span><span>have</span><span>(</span><span>)</span><span>)</span><span>:</span> <span>?></span></span> <span spellcheck="true">//这句也可以写成 if (count($relatedPosts->stack))</span>
<span><span><?php</span> <span>while</span> <span>(</span><span>$relatedPosts</span><span>-</span><span>></span><span>next</span><span>(</span><span>)</span><span>)</span><span>:</span> <span>?></span></span>
<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$relatedPosts</span><span>-</span><span>></span><span>permalink</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span> <span>title</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$relatedPosts</span><span>-</span><span>></span><span>title</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span><span>></span></span></span><span><span><?php</span> <span>$relatedPosts</span><span>-</span><span>></span><span>title</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>a</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span>
<span><span><?php</span> <span>endwhile</span><span>;</span> <span>?></span></span>
<span><span><?php</span> <span>else</span> <span>:</span> <span>?></span></span>
<span><span><span><span><</span>li</span><span>></span></span></span>无相关文章<span><span><span><span></</span>li</span><span>></span></span></span>
<span><span><?php</span> <span>endif</span><span>;</span> <span>?></span></span></code></pre><p>28、隐藏head区域的程序版本和模版名称</p><pre><code><span><span><?php</span> <span>$this</span><span>-</span><span>></span><span>header</span><span>(</span><span>"generator=&template="</span><span>)</span><span>;</span> <span>?></span></span></code></pre><p>29、获取读者墙</p><pre><code><span><span><?php</span>
$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</span> <span>as</span> <span>$count){
$avatar</span> <span>=</span> <span>$avatar_path.md5(strtolower($count['mail'])).'.jpg';
$c_url</span> <span>=</span> <span>$count['url'];if(!$c_url</span> <span>)</span> <span>$c_url=Helper::options()->siteUrl;
$mostactive</span> <span>.</span><span>=</span> "<span><a href='" . $c_url . "' title='" . $count['author'] . " (参与" . $count['cnt'] . "次互动)' target='_blank'><img src='" . $avatar . "' alt='" . $count['author'] . "的头像' width='32' height='32' /></a>\<span>n";
}
echo$mostactive</span><span>;</span> <span>?></span></span></code></pre><p>30、登陆与未登录用户展示不同内容</p><pre><code><span><span><?php</span> <span>if</span><span>(</span><span>$this->user->hasLogin()):?>
登陆可见
<?phpelse:?>
未登录和登陆均可见
<?phpendif;?>
27、导航页面列表调用隐藏特定的页面 这个演示隐藏了album和search两个页面
<ul>
<li<?php if($this</span><span>-</span><span>></span><span>is</span><span>(</span><span>'index'</span><span>)</span><span>)</span><span>:</span> <span>?></span></span><span><span><?php</span> <span>endif</span><span>;</span> <span>?></span></span>></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$this->options->siteUrl(); ?>">主页</a></li>
<?php $this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Contents_Page_List'</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$pages); ?><span><span><?php</span> <span>while</span><span>(</span><span>$pages</span><span>-</span><span>></span><span>next</span><span>(</span><span>)</span><span>)</span><span>:</span> <span>?></span></span>
<span><span><?php</span> <span>if</span> <span>(</span><span>(</span><span>$pages</span><span>-</span><span>></span><span>slug</span> <span>!=</span> <span>'album'</span><span>)</span> <span>&&</span> <span>(</span><span>$pages</span><span>-</span><span>></span><span>slug</span> <span>!=</span> <span>'search'</span><span>)</span><span>)</span><span>:</span> <span>?></span></span>
<span><li<span><span><?php</span> <span>if</span><span>(</span><span>$this</span><span>-</span><span>></span><span>is</span><span>(</span><span>'page'</span><span>,</span> <span>$pages</span><span>-</span><span>></span><span>slug</span><span>)</span><span>)</span><span>:</span> <span>?></span></span><span><span><?php</span> <span>endif</span><span>;</span> <span>?></span></span>></span><span><span><span><span><</span>a</span> <span>href</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$pages</span><span>-</span><span>></span><span>permalink</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span> <span>title</span><span><span>=</span><span>"</span><span><span><?php</span> <span>$pages</span><span>-</span><span>></span><span>title</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span>"</span></span><span>></span></span></span><span><span><?php</span> <span>$pages</span><span>-</span><span>></span><span>title</span><span>(</span><span>)</span><span>;</span> <span>?></span></span><span><span><span><span></</span>a</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span>
<span><span><?php</span> <span>endif</span><span>;</span> <span>?></span></span>
<span><span><?php</span> <span>endwhile</span><span>;</span> <span>?></span></span>
</ul>
参数说明
9.0版typecho支出在后台管理页面编辑时选择隐藏页面
27、Typecho归档页面(牧风提供)
<?php $this</span><span>-</span><span>></span><span>widget</span><span>(</span><span>'Widget_Contents_Post_Recent'</span><span>,</span> <span>'pageSize=10000'</span><span>)</span><span>-</span><span>></span><span>to</span><span>(</span><span>$archives);<span>$year</span><span>=</span><span>0</span><span>;</span> <span>$mon</span><span>=</span><span>0</span><span>;</span> <span>$i</span><span>=</span><span>0</span><span>;</span> <span>$j</span><span>=</span><span>0</span><span>;</span>
<span>$output</span> <span>=</span> '<span><span><span><span><</span>div</span> <span>id</span><span><span>=</span><span>"</span>archives<span>"</span></span><span>></span></span></span>'<span>;</span>
<span>while</span><span>(</span><span>$archives</span><span>-</span><span>></span><span>next</span><span>(</span><span>)</span><span>)</span><span>:</span>
<span>$year_tmp</span> <span>=</span> <span>date</span><span>(</span><span>'Y'</span><span>,</span><span>$archives</span><span>-</span><span>></span><span>created</span><span>)</span><span>;</span>
<span>$mon_tmp</span> <span>=</span> <span>date</span><span>(</span><span>'m'</span><span>,</span><span>$archives</span><span>-</span><span>></span><span>created</span><span>)</span><span>;</span>
<span>$y</span><span>=</span><span>$year</span><span>;</span> <span>$m</span><span>=</span><span>$mon</span><span>;</span>
<span>if</span> <span>(</span><span>$mon</span> <span>!=</span> <span>$mon_tmp</span> <span>&&</span> <span>$mon</span> <span>></span> <span>0</span><span>)</span> <span>$output</span> <span>.</span><span>=</span> '<span><span><span><span></</span>ul</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span>'<span>;</span>
<span>if</span> <span>(</span><span>$year</span> <span>!=</span> <span>$year_tmp</span> <span>&&</span> <span>$year</span> <span>></span> <span>0</span><span>)</span> <span>$output</span> <span>.</span><span>=</span> '<span><span><span><span></</span>ul</span><span>></span></span></span>'<span>;</span>
<span>if</span> <span>(</span><span>$year</span> <span>!=</span> <span>$year_tmp</span><span>)</span> <span>{</span>
<span>$year</span> <span>=</span> <span>$year_tmp</span><span>;</span>
<span>$output</span> <span>.</span><span>=</span> '<span><span><span><span><</span>h3</span> <span>class</span><span><span>=</span><span>"</span>al_year<span>"</span></span><span>></span></span></span><span>'. $year .'</span> 年<span><span><span><span></</span>h3</span><span>></span></span></span><span><span><span><span><</span>ul</span> <span>class</span><span><span>=</span><span>"</span>al_mon_list<span>"</span></span><span>></span></span></span>'<span>;</span> <span spellcheck="true">//输出年份</span>
<span>}</span>
<span>if</span> <span>(</span><span>$mon</span> <span>!=</span> <span>$mon_tmp</span><span>)</span> <span>{</span>
<span>$mon</span> <span>=</span> <span>$mon_tmp</span><span>;</span>
<span>$output</span> <span>.</span><span>=</span> '<span><span><span><span><</span>li</span><span>></span></span></span><span><span><span><span><</span>span</span> <span>class</span><span><span>=</span><span>"</span>al_mon<span>"</span></span><span>></span></span></span><span>'. $mon .'</span> 月<span><span><span><span></</span>span</span><span>></span></span></span><span><span><span><span><</span>ul</span> <span>class</span><span><span>=</span><span>"</span>al_post_list<span>"</span></span><span>></span></span></span>'<span>;</span> <span spellcheck="true">//输出月份</span>
<span>}</span>
<span>$output</span> <span>.</span><span>=</span> '<span><span><span><span><</span>li</span><span>></span></span></span><span>'.date('</span>d日<span>:</span> <span>',$archives->created).'</span><span><a href="'.$archives-></span>permalink <span>.</span><span>'">'</span><span>.</span> <span>$archives</span><span>-</span><span>></span><span>title</span> <span>.</span>'<span><span><span><span></</span>a</span><span>></span></span></span> <span><span><span><span><</span>em</span><span>></span></span></span><span>(</span><span>'. $archives->commentsNum.'</span><span>)</span><span><span><span><span></</span>em</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span>'<span>;</span> <span spellcheck="true">//输出文章日期和标题</span>
<span>endwhile</span><span>;</span>
<span>$output</span> <span>.</span><span>=</span> '<span><span><span><span></</span>ul</span><span>></span></span></span><span><span><span><span></</span>li</span><span>></span></span></span><span><span><span><span></</span>ul</span><span>></span></span></span><span><span><span><span></</span>div</span><span>></span></span></span>'<span>;</span>
<span>echo</span> <span>$output</span><span>;</span>
?>