侧边栏壁纸

Typecho一些比较常用的语法调用变量

2023年06月20日 256阅读 0评论 0点赞

日常写作和主题微调中,可能会用到一些常用的Typecho语法,这里我汇总些,方便大家和自己使用。

⭐ 站点名称

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

⭐ 站点网址

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

⭐ 后台网址

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

⭐ 文章标题

<?php $this->archiveTitle(' &raquo; ', '', ); ?>

⭐ 文章链接带超级链

<span class="text"><a class="link" href="<?php $this->permalink() ?>" target="_blank" rel="noopener noreferrer nofollow"><?php $this->permalink() ?></a></span>

⭐ 作者头像

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

此处输出的完整的img标签,40是头像的宽和高。

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

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

⭐ 该文作者个人主页链接

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

⭐ 该文作者的邮箱地址

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

⭐ 上一篇与下一篇调用代码

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

⭐ 文章/页面评论数目

<?php $this->commentsNum('No Comments', '1 Comment' , '%d Comments'); ?>

⭐ 截取文章内容显示摘要

<?php $this->excerpt(350, '.. .'); ?>

要显示的摘要数字直接修改350

⭐ 调用自定义字段

<?php $this->fields->fieldName ?>

⭐ 当前登录的用户名

<?php $this->user->screenName(); ?>

⭐ 退出链接

<a href="<?php $this->options->logoutUrl(); ?>"><?php _e('退出'); ?></a>

⭐ RSS 地址

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

⭐ 获取最新 Post

<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('<li><a href="{permalink}">{title}</a></li>'); ?>

⭐ 获取最新评论列表

<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; ?>

⭐ 文章字数统计

在 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); ?>

⭐ Type­cho 归档页面

<?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;?>

未完,持续更新。。。

0
打赏

—— 评论区 ——

昵称
邮箱
网址
取消