Typecho 主题模版制作常用调用变量和函数,参数分享

No Reply , Posted in 代码 on January 26, 2015

博主对Typecho博客程序情有独钟,但是还不会PHP所以四处收集Typecho制作模板的代码和函数什么的。
一个是方便自己使用,另外一个方便大家查看,又找到了点代码,今天发一下。

站点名称


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

域名地址


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

后台地址


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

完整路径标题,比如 文章 站点


<?php $this->archiveTitle(' » ', '', ' - '); ?><?php $this->options->title(); ?>

站点说明


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

模版文件夹地址


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

作者名字


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

当前登陆用户名字


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

退出链接


<?php _e('退出'); ?>

rss地址


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

作者头像


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

参数说明

此函数是完整 img 标签,200代表长和宽

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


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

该文作者个人主页链接


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

该文作者的邮箱地址


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

rss评论


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

引用模版文件夹内php文件


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

参数说明

可以使用相对路径获取上级目录php文件

获取最新post


<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=8&type=category')->parse('
  • {title}
  • '); ?>

    纯文字分类名称,不带链接


    <?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}
    • '); ?>

    获取最新评论列表


      <?php $this->widget('Widget_Comments_Recent')->to($comments); ?> <?php while($comments->next()): ?>
    • <?php $comments->author(false); ?>: <?php $comments->excerpt(50, '...'); ?>
    • <?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()): ?>
        
  • <?php $comments->author(false); ?>: <?php $comments->excerpt(50, '...'); ?>
  • <?php endwhile; ?>

    获取文章时间归档


      <?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()): ?>
    <?php $tags->name(); ?>
    <?php endwhile; ?>
    

    文章循环


    <?php while($this->next()): ?>
    

    <?php endwhile; ?>

    调用该文相关文章列表


    <?php $this->related(5)->to($relatedPosts); ?>
        <?php if ($relatedPosts->have()): ?>    //这句也可以写成 if (count($relatedPosts->stack))
        <?php while ($relatedPosts->next()): ?>
            
  • <?php $relatedPosts->title(); ?>
  • <?php endwhile; ?> <?php else : ?>
  • 无相关文章
  • <?php endif; ?>

    各种列表页面标题,如标签分类


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

    文章或页面,标题


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

    文章上一篇


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

    文章下一篇


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

    文章或页面,链接


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

    文章或页面,发表时间


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

    文章或页面,评论数目


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

    文章或页面,内容,括号里有内容,如果加入了more就会自动生成链接


    <?php $this->content('阅读剩余部分...'); ?>
    

    文章所在分类,链接形式


    <?php $this->category(','); ?>
    

    文章,所加标签


    <?php $this->tags(', ', true, 'none'); ?>
    

    列表页分页


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

    隐藏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 .= "" . $count[\n";
    }
    echo $mostactive; ?>
    

    登陆与未登录用户展示不同内容


    <?php if($this->user->hasLogin()): ?>
    登陆可见
    <?php else: ?>
    未登录和登陆均可见
    <?php endif; ?>
    

    导航页面列表调用隐藏特定的页面 这个演示隐藏了album和search两个页面


      is('index')): ?> class="current"<?php endif; ?>>主页 <?php $this->widget('Widget_Contents_Page_List')->to($pages); ?> <?php while($pages->next()): ?> <?php if (($pages->slug != 'album') && ($pages->slug != 'search')): ?> is('page', $pages->slug)): ?> class="current"<?php endif; ?>><?php $pages->title(); ?> <?php endif; ?> <?php endwhile; ?>

    参数说明

    9.0版typecho支出在后台管理页面编辑时选择隐藏页面

    Typecho归档页面(牧风提供,牧风演示:http://mufeng.me/archives)


    <?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
        $year=0; $mon=0; $i=0; $j=0;
        $output = '
    '; 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 .= ''; if ($year != $year_tmp && $year > 0) $output .= ''; if ($year != $year_tmp) { $year = $year_tmp; $output .= '

    '. $year .' 年

      '; //输出年份 } if ($mon != $mon_tmp) { $mon = $mon_tmp; $output .= '
    • '. $mon .' 月
        '; //输出月份 } $output .= '
      • '.date('d日: ',$archives->created).''. $archives->title .' ('. $archives->commentsNum.')
      • '; //输出文章日期和标题 endwhile; $output .= '
    '; echo $output; ?>

    以上内容来自:http://yijile.com/log/185.html

    标签: 函数, 变量, Typecho