Typecho 常用调用语法

972天前1791

以下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> 

* 若非特殊说明,本站文章均为博主原创,码字不易,如需转载,请注明出处!有疑问可留言交流,谢谢。

Typecho0 

Typecho 常用调用语法 - Jdeal | Life is like a Design.