以下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(); ?>
获取最新评论列表
<?php $this->widget('Widget_Comments_Recent')->to($comments); ?> <?php while($comments->next()): ?>
{date}
'); ?>
获取标签集合
<?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>
获取文章分类列表
<!-- 获取文章分类 -->
<ul>
<?php $this->widget('Widget_Metas_Category_List')
->parse('<li><a href="{permalink}">{name}</a> ({count})</li>'); ?>
</ul>
<!-- 获取文章分类结束 -->
获取最新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 $this->widget('Widget_Metas_Category_List')
->parse('
- {name} ({count}) '); ?>
获取某分类post
-
<?php
$this->widget('Widget_Archive@indexyc', 'pageSize=8&type=category', 'mid=1')
->parse('
- {title} '); ?>
22、获取最新评论列表
<?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; ?>
首页获取 最新文章 代码限制条数
<?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; ?>
25、获取文章时间归档
-
<?php $this->widget('Widget_Contents_Post_Date', 'type=month&format=F Y')
->parse('
- {date} '); ?>
获取标签集合,也就是标签云
<?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))
<?php while ($relatedPosts->next()): ?>
<li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
<?php endwhile; ?>
<?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; ?>
导航页面列表调用隐藏特定的页面 这个演示隐藏了album和search两个页面
-
<li<?php if($this->is('index')): ?> class="current"<?php endif; ?>><a href="<?php $this->options->siteUrl(); ?>">主页
<?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; ?>>
<?php endif; ?>
<?php endwhile; ?>
参数说明
9.0版typecho支出在后台管理页面编辑时选择隐藏页面
Typecho归档页面(牧风提供)
<?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;
?>
Comments | NOTHING
该文章已经关闭评论