Typecho创建自定义模板的教程

之前用Typecho程序搭建过一个网站,给我的感觉就是简洁,真的是太简洁了,随着看别人的博客,看到很多Typecho程序搭建的网站有很好看的模板的,就又研究了一下这个程序。本文从别人的网站上和自己搜索的一些Typecho创建自定义模板的教程的方法。

自定义首页模板

在当前模板目录下面建你需要的文件(例如:home.php),然后再文件的开头加上如下代码(需在 package 后面加上 index)就算是自定义了好了一个首页:

<?php
/**
* 自定义首页模板
*
* @package index
*/

然后进入后台的『设置』-『文章』页面,选择“站点首页”中的“直接调用[home.php]模板文件”,保存即可。

自定义页面(page)模板

只需要在当前模板目录下面建你需要的文件,然后再文件的开头加上如下代码(需在 package 后面加上 custom)就算是自定义了好了一个页面,可以自定义多个页面:

<?php
/**
* 自定义页面模板
*
* @package custom
*/

其中@package custom是必须的,然后进入后台在『创建页面』的【展开高级选项】里就可以看到。

自定义分类模板

方法一:直接在当前模板目录下建立一个名为 category 的目录,然后在里面放上需要单独做模板分类的缩略名为文件名的 php 文件,比如 default.php,这样,在访问缩略名为default的分类时,它会自动调用这个模板。

方法二:在模板文件中使用 is 语法判断页面

<?php if ($this->is('category', 'default')): ?>
//默认分类模板
<?php endif; ?>
<?php if ($this->is('category', 'category2')): ?>
//分类2模板
<?php endif; ?>

自定义页面列表显示条数

模板目录下建立一个名为 functions.php 的文件 然后里面写一个函数(示例是控制 jobs 分类下的文章列表显示条数为 10 条)

function themeInit($archive) {
if ($archive->is('category', 'jobs')) {
$archive->parameter->pageSize = 10; // 自定义条数
}
}

拓展:Typecho自定义首页keywords和Description内容

使用如下代码,可以自定义相关header的Keywords和Description内容:

<?php if($this->is('index')): ?>
<?php $this->header('description=Hello Typecho,收集Typecho相关代码,模板,插件。'); ?>
<?php else: ?>
<?php $this->header(); ?><?php endif; ?>

Typecho自定义评论样式

可以设置自定义的评论样式,具体代码如下:

<?php
function threadedComments($comments,$singleCommentOptions) {
$imgUrl = Helper::options()->siteUrl . 'usr/themes/thinkH/images/pingback.jpg';
$author = '<a href="'.$comments->url.'" rel="external nofollow" target="_blank">'.$comments->author.'';
$depth = $comments->levels +1;
if($depth<=1){
$size = (int) Helper::options()->commentsPageSize;
$curr = (int) $comments->currentPage;//这个数值为0,暂不清楚是干什么的,但我需要一个输出当前页数的函数!!!
$cn   = (int) $comments->sequence;
$n = $cn + $curr*$size;
$floor = 'For All '.$n.' The-FatherC'; // 主楼层
}else{
$floor = ($depth-1).'#InnerC';  // 子楼层
$style = 'style="margin-left:' . ceil(5/$depth) . 'px;"';
}
?>
<li id="<?php $comments->theId(); ?>" <?php if( $depth > 1) echo $style;?>>
<div id="comment-<?php $comments->theId(); ?>">
<div  class="comment-body <?php echo 'depth-'.$depth;?>">
<div class="commentmeta <?php echo 'commentmeta-'.$depth;?>"></div>
<div class="comment-author">
<?php
$tLC = (Helper::options()->ShGravatar)?(Helper::options()->ShGravatar):0;
if ($tLC == 0) {
?>
<?php include("include/avatar.php"); ?>
<?php }else{?>
<?php $comments->gravatar(32, 'identicon', 'X', 'avatar'); ?>
<?php } ?>
<cite class="fn"><?php commentApprove($comments, $comments->mail); ?><?php echo $author; ?></cite>
<span><?php $comments->date('Y-m-d H:i:s') ?></span>  
<span class="subcolor">This</span><span style="margin-left:5px;color:#617d0e;font-size:12px">
<?php if($comments->levels == 0): ?>
<?php if($comments->sequence == 1): ?>沙发
<?php elseif($comments->sequence == 2): ?>板凳
<?php elseif($comments->sequence == 3): ?>地毯
<?php elseif($comments->sequence == 4): ?>门口
<?php elseif($comments->sequence == 5): ?>走廊
<?php elseif($comments->sequence == 6): ?>楼梯
<?php elseif($comments->sequence == 7): ?>宅院
<?php elseif($comments->sequence == 8): ?>小区
<?php elseif($comments->sequence == 9): ?>街道
<?php else: ?>
第<?php  $comments->sequence(); ?>号大院<?php endif; ?>
<?php endif; ?>
</span><span class="subcolor"><?php echo $floor; ?></span>
<span class="comment-reply">
<?php $comments->reply('reply'); ?>
</span>
<div class="subcolor ">
[<?php GetBrowser($comments->agent)?> @ <?php GetOs($comments->agent)?>]
</div>
</div>
<div class="comment_content"><?php $comments->content(); ?></div>
</div>
<div class="children">
<?php if ($comments->children) { ?><?php $comments->threadedComments($singleCommentOptions); ?><?php } ?>
</div>
</div>
</li>
<?php
}
?>

typecho可以使用is语法判断很多东西:

比如

$this->is('index');
$this->is('archive');
$this->is('single');
$this->is('page');
$this->is('post');
$this->is('category');
$this->is('tag');

或者是:

$this->is('category', 'default');
$this->is('page', 'start');
$this->is('post', 1);

需要注意的是,后面的参数是分类、页面的缩略名,写法如:

<?php if ($this->is('post')) : ?>
// 这里就是内容了
<?php endif; ?>
Typecho

👋 感谢您的观看!

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享