Typecho文章归档教程(非插件)

Typecho创建文章归档页面

代码

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

本站的归档用的是插件调用出来的,但是个人认为这篇文章的代码更为简洁一些!

具体步骤

第一步先复制主题的page.php,然后重命名成你喜欢的归档页名字如:archives.php

第二步将archives.php中的header调用函数给改了吗,将下方代码

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

替换成

<?php   
    /**  
    * archives  
    *  
    * @package custom  
    */  
    $this->need('header.php'); ?>

这个archives指的是模板名字,必须修改,否则后台创建页面没法选择模板了!

然后将

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

替换为我一开始给出的代码

到此归档页面已经创建完成,接著上传到主题目录——>然后进入typecho后台——>新建页面——>选择模板为archives,如果开始创建这个页面的时候不是取的这个名字请自行选择!

到这创建归档页面的教程就结束了,如有其他疑问博主会在评论区解答