typecho模板中常用的调用函数


typecho模板中常用的调用函数

常规调用

站点名称(其实就是调用title标签的内容的)

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

站点网址(其实就是调用首页名称的)

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

站点描述调用

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

完整路径标题(其实就是调用面包屑导航的)

<?php $this->archiveTitle(' &raquo; ', < span class="string">'', ' | '); ?><?php $this ->options->title(); ?>

模板文件夹地址(博主基本不用,因为不知道要这个是干嘛)

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

输出指定的php文件,一般起关联作用,起到组合作用

<?php $this->need('global.php'); ?>

输出作者字段

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

输出头像(此处40为img元素的宽度和高度)

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

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

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

该文作者个人主页链接

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

该文作者的邮箱地址

<?php $this->author->mail(); ?>

typecho 默认的文章上下翻页的调用方法

<?php $this->thePrev(); ?>

<?php $this</span>-&gt;theNext(); <span>?&gt;</span></code></pre><p>判断是否为首页,输出相关内容(博主不常用)</p><pre><code><span>&lt;?php</span> <span>if</span> (<span>$this->is('index')): ?>
//首页输出内容
<?php else: ?>
//不是首页输出内容
< span><?php endif; ?>

文章列表或页面,评论数目输出方法

<?php $this</span>-&gt;commentsNum(<span>'No Comments'</span>, <span>'1 Comment'</span> , <span>'%d Comments'</span>); <span>?&gt;</span></code></pre><p>截取部份文章简称“摘要”,200是字数限制</p><pre><code><span>&lt;?php</span> <span>$this->excerpt(200, '.. .'); ?>

调用自定义字段(官方文档坑爹,竟然没有,博主自己摸索出来的)

<?php $this</span>-&gt;fields-&gt;fieldName <span>?&gt;</span></code></pre><p>RSS订阅地址输出方法</p><pre><code><span>&lt;?php</span> <span>$this->options->feedUrl(); ?>

获取最新post

<?php $this</span>-&gt;widget(<span>'Widget_Contents_Post_Recent'</span>, <span>'pageSize=8&amp;type=category'</span>)-&gt;parse(<span>'&lt;li&gt;&lt;a href="{permalink}"&gt;{title}&lt;/a&gt;&lt;/li&gt;'</span>); <span>?&gt;</span></code></pre><p>纯文字分类名称,不带链接</p><pre><code><span>&lt;?php</span> <span>$this->category(',', false); ?>

获取文章分类列表

<ul>
<?php $this->widget('Widget_Metas_Category_List')

            -&gt;parse(<span>'&lt;li&gt;&lt;a href="{permalink}"&gt;{name}&lt;/a&gt; ({count})&lt;/li&gt;'</span>); <span>?&gt;</span>

</ul>
// count 输出分类中文章的个数
// name 输出分类名称

获取分类列表,带高亮,看好,是带高亮的分类导航

<?php $this</span>-&gt;widget(<span>'Widget_Metas_Category_List'</span>)-&gt;to($category); ?>
<?php while($category->next()): ?>
<a<?php if($this</span>-&gt;is(<span>'category'</span>, $category->slug)): ?> class="active"<?php endif; ?> href="<?php $<span>category</span>-&gt;<span>permalink</span>(); ?&gt;"&gt;&lt;?<span>php</span> $category->name(); ?></a>
<?php endwhile; ?>

获取某分类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>

获取最新评论列表

// 版本A,获取最新评论,但是包含了博主的评论在里面了。
<ul>

<span>&lt;?php</span> <span>$this</span>-&gt;widget(<span>'Widget_Comments_Recent'</span>)-&gt;to($comments); <span>?&gt;</span>
<span>&lt;?php</span> <span>while</span>($comments-&gt;next()): <span>?&gt;</span>
    &lt;li&gt;&lt;a href=<span>"&lt;?php $comments-&gt;permalink(); ?&gt;"</span>&gt;<span>&lt;?php</span> $comments-&gt;author(<span>false</span>); <span>?&gt;</span>&lt;/a&gt;: <span>&lt;?php</span> $comments-&gt;excerpt(<span>50</span>, <span>'...'</span>); <span>?&gt;</span>&lt;/li&gt;
<span>&lt;?php</span> <span>endwhile</span>; <span>?&gt;</span>

</ul>

// 版本B,获取最新评论,不包含作者的评论。
<?php $this</span>-&gt;widget(<span>'Widget_Comments_Recent'</span>,<span>'ignoreAuthor=true'</span>)-&gt;to($comments); ?>
<?php while($comments->next()): ?>

&lt;div <span><span>class</span>="<span>list</span>-<span>group</span> <span>list</span>-<span>group</span>-<span>alt</span>"&gt; 
  &lt;<span>a</span> <span>href</span>="&lt;?<span>php</span> $<span>comments</span>-&gt;<span>permalink</span>(); ?&gt;" <span>class</span>="<span>media</span> <span>list</span>-<span>group</span>-<span>item</span>"&gt; 
  &lt;<span>span</span> <span>class</span>="<span>pull</span>-<span>left</span> <span>thumb</span>-<span>sm</span>"&gt; &lt;?<span>php</span> $<span>comments</span>-&gt;<span>gravatar</span>('40', ''); ?&gt; &lt;/<span>span</span>&gt; 
  &lt;<span>span</span> <span>class</span>="<span>media</span>-<span>body</span> <span>block</span> <span>m</span>-<span>b</span>-<span>none</span>"&gt;&lt;?<span>php</span> $<span>comments</span>-&gt;<span>author</span>(<span>false</span>); ?&gt;&lt;<span>br</span> /&gt; 
    &lt;<span>small</span> <span>class</span>="<span>text</span>-<span>muted</span>"&gt;&lt;?<span>php</span> $<span>comments</span>-&gt;<span>excerpt</span>(50, '...'); ?&gt;&lt;/<span>small</span>&gt; 
  &lt;/<span>span</span>&gt; 
  &lt;/<span>a</span>&gt; 
 &lt;/<span>div</span>&gt; 

<?php endwhile; ?>

首页获取最新文章(带条数限制)

<?php while ($this->next()): ?>
<?php if ($this->sequence <= 3): ?>

<span>// 中间可使用的输出方法</span>
<span>&lt;?php</span> <span>$this</span>-&gt;permalink() <span>?&gt;</span> <span>// 输出文章链接</span>
<span>&lt;?php</span> <span>$this</span>-&gt;title() <span>?&gt;</span> <span>// 输出标题</span>

<?php endif; ?>
<?php endwhile; ?>

相关文章输出

<?php $this</span>-&gt;related(<span>5</span>)-&gt;to($relatedPosts); ?>

<span>&lt;?php</span> <span>if</span> ($relatedPosts-&gt;have()): <span>?&gt;</span>    <span>//这句也可以写成 if (count($relatedPosts-&gt;stack))</span>
<span>&lt;?php</span> <span>while</span> ($relatedPosts-&gt;next()): <span>?&gt;</span>
    &lt;li&gt;&lt;a href=<span>"&lt;?php $relatedPosts-&gt;permalink(); ?&gt;"</span> title=<span>"&lt;?php $relatedPosts-&gt;title(); ?&gt;"</span>&gt;<span>&lt;?php</span> $relatedPosts-&gt;title(); <span>?&gt;</span>&lt;/a&gt;&lt;/li&gt;
<span>&lt;?php</span> <span>endwhile</span>; <span>?&gt;</span>
<span>&lt;?php</span> <span>else</span> : <span>?&gt;</span>
    &lt;li&gt;无相关文章&lt;/li&gt;
<span>&lt;?php</span> <span>endif</span>; <span>?&gt;</span></code></pre><p>隐藏head区域的程序版本和模版名称(博主一向尊重版权与知识归属,不会使用这个的)</p><pre><code><span>&lt;?php</span> <span>$this</span>-&gt;header(<span>"generator=&amp;template="</span>); <span>?&gt;</span></code></pre><p>登陆与未登录用户展示不同内容(考虑到注入的风险,作者暂时没想过开放注册)</p><pre><code><span>&lt;?php</span> <span>if</span>(<span>$this</span>-&gt;user-&gt;hasLogin()): <span>?&gt;</span>
<span>// 登陆可见</span>

<?php else: ?>

<span>// 未登录和登陆均可见</span>

<?php endif; ?>

导航页面列表调用隐藏特定的页面 这个演示隐藏了album和search两个页面(1.0及1.1版本后台自己就支持隐藏,所以,可以忽略)

<ul>
<li<?php if($this</span>-&gt;is(<span>'index'</span>)): <span>?&gt;</span> <span><span>class</span>="<span>current</span>"&lt;?<span>php</span> <span>endif</span>; ?&gt;&gt;&lt;<span>a</span> <span>href</span>="&lt;?<span>php</span> $this->options->siteUrl(); ?>">主页</a></li>
<?php $<span>this</span>-&gt;<span>widget</span>('<span>Widget_Contents_Page_List</span>')-&gt;<span>to</span>($pages); ?>

&lt;?<span>php</span> <span>while</span>($<span>pages</span>-&gt;<span>next</span>()): ?&gt;
&lt;?<span>php</span> <span>if</span> (($<span>pages</span>-&gt;<span>slug</span> != '<span>album</span>') &amp;&amp; ($<span>pages</span>-&gt;<span>slug</span> != '<span>search</span>')): ?&gt;
&lt;<span>li</span>&lt;?<span>php</span> <span>if</span>($<span>this</span>-&gt;<span>is</span>('<span>page</span>', $<span>pages</span>-&gt;<span>slug</span>)): ?&gt; <span>class</span>="<span>current</span>"&lt;?<span>php</span> <span>endif</span>; ?&gt;&gt;&lt;<span>a</span> <span>href</span>="&lt;?<span>php</span> $<span>pages</span>-&gt;<span>permalink</span>(); ?&gt;" <span>title</span>="&lt;?<span>php</span> $<span>pages</span>-&gt;<span>title</span>(); ?&gt;"&gt;&lt;?<span>php</span> $<span>pages</span>-&gt;<span>title</span>(); ?&gt;&lt;/<span>a</span>&gt;&lt;/<span>li</span>&gt;
&lt;?<span>php</span> <span>endif</span>; ?&gt;
&lt;?<span>php</span> <span>endwhile</span>; ?&gt;

</ul>

后台地址输出

<?php $this</span>-&gt;options-&gt;adminUrl(); <span>?&gt;</span></code></pre><p>当前登录用户名称</p><pre><code><span>&lt;?php</span> <span>$this->user->screenName(); ?>

退出登录地址

<?php $this</span>-&gt;options-&gt;logoutUrl(); <span>?&gt;</span></code></pre><p>遍历输出文章</p><pre><code><span>&lt;?php</span> <span>while</span>(<span>$this->next()): ?>

<span>// 文章标题内容等</span>

<?php endwhile; ?>

RSS评论地址

<?php $this</span>-&gt;options-&gt;commentsFeedUrl(); <span>?&gt;</span></code></pre><p>文章列表及页面“阅读更多分界线”</p><pre><code><span>&lt;?php</span> <span>$this->content('阅读剩余部分'); ?>

文章所在分类输出

<?php $this</span>-&gt;category(<span>','</span>); <span>?&gt;</span></code></pre><p>文章标签调用</p><pre><code><span>&lt;?php</span> <span>$this->tags(', ', true, 'none'); ?>

全部文章列表输出,可应用于归档或网站地图,蜘蛛指引

<?php $this</span>-&gt;widget(<span>'Widget_Contents_Post_Recent'</span>, <span>'pageSize=10000'</span>)-&gt;parse(<span>'&lt;li&gt;{year}-{month}-{day} : &lt;a href="{permalink}"&gt;{title}&lt;/a&gt;&lt;/li&gt;'</span>); <span>?&gt;</span></code></pre><p>全部标签列表,按照MID排序</p><pre><code><span>&lt;?php</span> <span>$this->widget('Widget_Metas_Tag_Cloud')
->to($taglist); <span>?&gt;</span><span>&lt;?php</span> <span>while</span>($taglist->next()): ?>
<li><a href="<?php $taglist-&gt;permalink(); ?&gt;"</span> title=<span>"&lt;?php $taglist->name(); ?>"><?php $taglist->name(); ?></a></li>
<?php endwhile; ?>

自定义标签数量(就这里面的20),按照文章数量排序

<?php $this</span>-&gt;widget(<span>'Widget_Metas_Tag_Cloud'</span>, <span>array</span>(<span>'sort'</span> =&gt; <span>'count'</span>, <span>'ignoreZeroCount'</span> =&gt; <span>true</span>, <span>'desc'</span> =&gt; <span>true</span>, <span>'limit'</span> =&gt; <span>20</span>))-&gt;to($tags); ?>
<?php while($tags->next()): ?>
<li><a rel="tag" href="<?php $tags-&gt;permalink(); ?&gt;"</span>&gt;<span>&lt;?php</span> $tags->name(); ?></a></li>
<?php endwhile; ?>

自定义分类、标签、搜索、首页等文章分页数量,修改 functions.php 文件

function themeInit($archive) {
if ($archive->is('index')) {
$archive->parameter->pageSize = 10; // 自定义条数
}
}
或者:
function themeInit($archive) {
if ($archive->is('category', 'default')) {
$archive->parameter->pageSize = 10; // 自定义条数
}
}

调用某分类文章,pageSize是数量,mid是分类号

<?php $this->widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=47')
->parse('<li><a href="{permalink}">{title}</a></li>'); ?>

判断为当前页的第几篇文章,并单独输出代码,可应用于第一篇文章底部广告

<?php if ($this->sequence == 0): ?>

<span>//需要的插入</span>

<?php endif; ?>

判断当前分类,输出内容

<?php if($this->category == "help"): ?>

<span>//当前分类为help缩略图,则输出内容。</span>

<?php endif; ?>

首页不显示某分类内容(博主不用)

<?php while($this->next()): ?>
<?php if($this->category != "cateslug"): ?>

<span>//正常输出循环</span>

<?php endif; ?>
<?php endwhile; ?>

可自定文章列表首篇显示样式

<?php if (($this</span>-&gt;_currentPage == <span>1</span>) &amp;&amp; (<span>$this->sequence == 1)): ?>

 <span>//首页第一篇文章</span>

<?php else: ?>

 <span>//其它文章</span>

<?php endif; ?>

单独输出文字内容

<?php _e('MoleRose'); ?>

文章输出最后修改时间

<?php echo date('Y年m月d日 H:i A', $this</span>-&gt;modified);<span>?&gt;</span></code></pre><div name="页面制作" data-unique="页面制作"></div><h2>页面制作</h2><p>归档页面制作(推荐)</p><pre><code><span>&lt;?php</span> <span>$this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);

$year=<span>0</span>; $mon=<span>0</span>; $i=<span>0</span>; $j=<span>0</span>;
$output = <span>'&lt;div id="archives"&gt;&lt;dl&gt;'</span>;
<span>while</span>($archives-&gt;next()):
    $year_tmp = date(<span>'Y'</span>,$archives-&gt;created);
    $mon_tmp = date(<span>'m'</span>,$archives-&gt;created);
    $y=$year; $m=$mon;
    <span>// if ($mon != $mon_tmp &amp;&amp; $mon &gt; 0) $output .= '';</span>
    <span>// if ($year != $year_tmp &amp;&amp; $year &gt; 0) $output .= '';</span>
    <span>if</span> ($year != $year_tmp) {
        $year = $year_tmp;
        $output .= <span>'&lt;h3&gt;'</span>. $year .<span>' 年&lt;/h3&gt;'</span>; <span>//输出年份</span>
    }
    <span>if</span> ($mon != $mon_tmp) {
        $mon = $mon_tmp;
        $output .= <span>'&lt;dt&gt;&lt;small&gt;'</span>. $mon .<span>' 月&lt;/small&gt;&lt;/dt&gt;'</span>; <span>//输出月份</span>
    }
    $output .= <span>'&lt;dd&gt;&lt;a href="'</span>. $archives-&gt;permalink .<span>'" title="'</span>. $archives-&gt;title .<span>'"&gt;'</span>. $archives-&gt;title .<span>'&lt;div&gt;&lt;small&gt;&lt;i&gt;&lt;/i&gt;'</span>.date(<span>'于d日发布,'</span>,$archives-&gt;created).<span>'共'</span>. $archives-&gt;commentsNum.<span>'条评论&lt;/small&gt;&lt;/div&gt;&lt;/a&gt;&lt;/dd&gt;'</span>; <span>//输出文章日期和标题</span>
<span>endwhile</span>;
$output .= <span>'&lt;/dl&gt;&lt;/div&gt;'</span>;
<span>echo</span> $output;

?>

// 排版方式,可自行组合,上述输出是可以自由编辑样式的
<div>

<span>&lt;<span>h3</span>&gt;</span>2017年<span>&lt;/<span>h3</span>&gt;</span>
<span>&lt;<span>dl</span>&gt;</span>
    <span>&lt;<span>dt</span>&gt;</span>03月<span>&lt;/<span>dt</span>&gt;</span>
    <span>&lt;<span>dd</span>&gt;</span><span>&lt;<span>a</span> <span>href</span>=<span>"#"</span>&gt;</span>文章标题<span>&lt;/<span>a</span>&gt;</span><span>&lt;/<span>dd</span>&gt;</span>
<span>&lt;/<span>dl</span>&gt;</span>

</div>

// 时间轴调用,常规版/官方版
<ul>

<span><span>&lt;?php</span> <span>$this</span>-&gt;widget(<span>'Widget_Contents_Post_Date'</span>, <span>'type=month&amp;format=F Y'</span>)
            -&gt;parse(<span>'&lt;li&gt;&lt;a href="{permalink}"&gt;{date}&lt;/a&gt;&lt;/li&gt;'</span>); <span>?&gt;</span></span>

</ul>

标签云制作(非独立页面,博主自己是使用在模板的右侧边栏)

<?php $this</span>-&gt;widget(<span>'Widget_Metas_Tag_Cloud'</span>, <span>'ignoreZeroCount=1&amp;limit=28'</span>)-&gt;to($tags); ?>
<?php while($tags->next()): ?>
<a href="<?php $tags-&gt;permalink(); ?&gt;"</span> <span><span>class</span>="<span>size</span>-&lt;?<span>php</span> $tags->split(5, 10, 20, 30); ?>"><?php $tags->name(); ?></a>
<?php endwhile; ?>

/// size-size-<?php $tags->split(5, 10, 20, 30); ?> 是随机输出文字的大小,博主是已经删掉了,换上自己的统一类名了

读者墙制作(作者使用的都是插件的,不过也看个人)

<?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 <span>as</span> $count) {
$avatar = $avatar_path . md5(strtolower($count['mail'])) . '.jpg';
$c_url = $count['url']; if ( !$c_url ) $c_url = Helper::options()->siteUrl;
$mostactive .= <span>"&lt;a href='"</span> . $c_url . "' title='" . $count[<span>'author'</span>] . <span>" (参与"</span> . $count['cnt'] . "次互动)' target='_blank'><img src='" . $avatar . <span>"' alt='"</span> . $count['author'] . "的头像' width='32' height='32' /></a>n";
}
echo $mostactive; <span>?&gt;</span></code></pre><p>前台登录制作(不包含帐号密码输入错误的判断,相信你们的能力,自己赋id自己写)</p><pre><code>&lt;form role=<span>"form"</span> action=<span>"&lt;?php $this->options->loginaction(); ?>" method="post">
<div class="form-group">
<label>用户名</label>
<input type="text" id="name" name="name" class="form-control" placeholder="请输入用户名" required>
</div>
<div class="form-group">
<label>密码</label>
<input type="password" id="password" name="password" class="form-control" placeholder="请输入密码" required>
</div>
<button type="submit" class="btn btn-group-justified btn-success">提交登录</button>
<input type="hidden" name="referer" value="<?php $this->options->adminUrl(); ?>">
</form>

整站数据统计输出

<?php Typecho_Widget::widget('Widget_Stat')->to($stat); ?>
文章总数:<?php $stat->publishedPostsNum() ?>
分类总数:<?php $stat->categoriesNum() ?>
评论总数:<?php $stat->publishedCommentsNum() ?>
页面总数:<?php $stat-&gt;publishedPagesNum() <span>?&gt;</span>个</code></pre><p>单独输出单页列表</p><pre><code><span>&lt;?php</span> <span>$this->widget('Widget_Contents_Page_List')->to($pages); ?>
<?php while($pages->next()): ?>
<li><a class="auto" <?php if($<span>this</span>-&gt;<span>is</span>('<span>page</span>', $pages->slug)): ?> class="current"<?php endif; ?> href="<?php $<span>pages</span>-&gt;<span>permalink</span>(); ?&gt;" <span>title</span>="&lt;?<span>php</span> $pages->title(); ?>"><i class="fa fa-angle-right text-xs"></i><span><?php $pages->title(); ?></span></a></li>
<?php endwhile; ?>

写在最后

以上代码在使用过程中有疑问的,或者说有错误的也请指出,我好做调整及更新,所谓您的支持,才是我续博的最大动力。

发表于:

文章详情
作者: 八爷
分类: 随笔
标签:typecho 分享
评论:0 次

More close

评论已关闭

随机格言