一些常用的Typecho调用语法

发布时间:2021-04-08

以下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字段
自动调用img字段内容,如果没有,去文章搜索第1个图片。

 <?php if (array_key_exists('img',unserialize($this->___fields()))): ?><?php $this->fields->img(); ?><?php else: ?><?php
preg_match_all("/\&lt;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("/\&lt;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> 

打赏


哇 你要支持我吗?太慷慨了!谢谢
上一篇
下一篇