typecho模板常用代码

我看几乎每个用typecho的人的博客都有类似的文章,我因为懒,所以一直就没有写,今天特别无聊,就写写吧,争取写的全点。

站点动态标题

  1. <?php $this->archiveTitle(array(
  2. 'category'=>_t('分类 %s 下的文章'),
  3. 'search'=>_t('包含关键字 %s 的文章'),
  4. 'tag' =>_t('标签 %s 下的文章'),
  5. 'author'=>_t('%s 的主页')
  6. ), '', ' - '); ?>

站点地址,名称与描述

  1. <?php $this->options->siteUrl(); ?>//站点地址
  2. <?php $this->options->title(); ?>//名称
  3. <?php $this->options->description() ?>//描述

index.php文章循环输出

  1. <?php if ($this->have()): ?>//判断文章存在与否
  2. <?php while($this->next()): ?>//开始循环
  3. <a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>//文章标题和超链接
  4. <a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>//作者名称和超链接
  5. <?php $this->date('F j, Y'); ?>//文章发布时间
  6. <?php $this->category(','); ?>//文章分类
  7. <?php $this->commentsNum('%d Comments'); ?>//评论数量
  8. <?php $this->content('Continue Reading...'); ?>//阅读全文more语法截取缩略内容(可改为<?php $this->excerpt(140,'....'); ?>自动截取前140个字符,根据需要也可以改成 <?php $this->summary(); ?新版功能,自动输出内容中第一个块级元素中的内容>
  9. <?php endwhile; ?>//循环结束
  10. <?php else: ?>暂无与之相关文章<?php endif; ?>//判断结束

翻页代码

  1. <?php $this->pageNav('上一页', '下一页', '5', '……'); ?>//显示多个页码的
  2. <?php $this->pageLink('下一页','next'); ?>
  3. <?php $this->pageLink('上一页'); ?>//只显示上一页下一页

页码显示

  1. 当前页码:<?php if($this->_currentPage>1) echo $this->_currentPage; else echo 1;?>
  2. 总页码:<?php echo ceil($this->getTotal() / $this->parameter->pageSize); ?>

文章全文显示

  1. <?php $this->content(); ?>

登陆判断

  1. <?php if($this->user->hasLogin()):?>
  2. 登陆才可以看到这里的内容
  3. <?php endif;?>

文章作者名称与主页地址

  1. <a href="<?php $this->author->permalink(); ?>"><?php $this->author(); ?></a>

文章最后编辑时间

  1. <?php echo gmdate('Y-m-d H:i', $this->modified + Typecho_Widget::widget('Widget_Options')->timezone); ?>

当前文章id

  1. <?php $this->cid(); ?>

文章中的上一篇和下一篇

  1. 上一篇: <?php $this->thePrev('%s','没有了'); ?>
  2. 下一篇: <?php $this->theNext('%s','没有了'); ?>

文章标签

  1. <?php $this->tags(', ', true, 'none'); ?>
  2. 说明:(', ', true, 'none')第一个单引号间的逗号代表标签与标签的间隔用逗号隔开,true是标签以超链接形式输出,none为该文章没有标签时显示的提示信息。

用户昵称

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

后台地址与登陆地址

  1. <?php $this->options->adminUrl(); ?>//后台地址
  2. <?php $this->options->adminUrl('login.php'); ?>//登陆地址

分类描述

  1. <?php echo $this->getDescription(); ?>

标签相关文章

  1. <?php $this->related(5)->to($relatedPosts); ?>
  2. <ul>
  3. <?php while ($relatedPosts->next()): ?>
  4. <li><a href="<?php $relatedPosts->permalink(); ?>" title="<?php $relatedPosts->title(); ?>"><?php $relatedPosts->title(); ?></a></li>
  5. <?php endwhile; ?>
  6. </ul>

调用某分类文章,pageSize是数量,mid是分类号:

  1. <?php $this->widget('Widget_Archive@index', 'pageSize=6&type=category', 'mid=47′)
  2. ->parse('<li><a href="{permalink}">{title}</a></li>'); ?>

首行缩进问题,加入css实现

  1. .post-content p{
  2. text-indent: 2em; /*em是相对单位,2em即现在一个字大小的两倍*/
  3. }

最新文章

  1. <?php $this->widget('Widget_Contents_Post_Recent')->to($post); ?>
  2. <?php while($post->next()): ?>
  3. <a href=”<?php $post->permalink(); ?>” title=”<?php $post->title(); ?>>
  4. <?php $post->title(25, '…'); ?></a>
  5. <?php endwhile; ?>

神奇的is语法

  1. <?php if ($this->is('post')) : ?>
  2. 这里就是内容了
  3. <?php endif; ?>
  4. typecho可以使用is语法判断很多东西,比如
  5. $this->is('index');
  6. $this->is('archive');
  7. $this->is('single');
  8. $this->is('page');
  9. $this->is('post');
  10. $this->is('category');
  11. $this->is('tag');
  12. 甚至是
  13. $this->is('category', 'default');
  14. $this->is('page', 'start');
  15. $this->is('post', 1);

判断为当前页的第几篇文章,并单独输出内容

  1. <?php if ($this->sequence == 0): ?>
  2. //需要的插入
  3. <?php endif; ?>
文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/27
0 评论
3.4k

发表评论

!