1. 创建归档页面模板

在 Typecho 主题目录下创建一个名为 archives.php 的文件。

sudo nano archives.php

复制并且使用Ctrl+Shift+V粘贴以下代码:

<?php 
/**
 * 文章归档
 *
 * @package custom
 */
if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php $this->need('header.php'); ?>

<div class="col-mb-12 col-8" id="main" role="main">
    <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
        <header class="post-head">
            <h2 class="post-title">
                <a href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
            </h2>
        </header>
        <section class="post-content typo">
            <h3>分类列表</h3>
            <ul>
            <?php
            // 获取所有分类
            $this->widget('Widget_Metas_Category_List')->to($categories);
            $allCategories = array();

            // 收集所有分类数据
            while ($categories->next()) {
                $allCategories[$categories->mid] = array(
                    'name' => $categories->name,
                    'permalink' => $categories->permalink,
                    'description' => $categories->description,
                    'count' => $categories->count,
                    'parent' => $categories->parent,
                    'mid' => $categories->mid
                );
            }

            // 输出分类树
            function printCategoryTree($categories, $parentId = 0, $level = 0) {
                foreach ($categories as $category) {
                    if ($category['parent'] == $parentId) {
                        echo '<li style="margin-left: ' . ($level * 20) . 'px;">';
                        echo '<a href="' . $category['permalink'] . '" title="' . $category['description'] . '">' . $category['name'] . '</a> (' . $category['count'] . ')';
                        if ($category['description']) {
                            echo ' --> ' . $category['description'];
                        }
                        echo '</li>';
                        // 递归显示子分类
                        printCategoryTree($categories, $category['mid'], $level + 1);
                    }
                }
            }
            printCategoryTree($allCategories);
            ?>
            </ul>

            <h3>文章归档</h3>
            <?php
            $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
            $year=0; $mon=0; $i=0; $j=0;
            $all = array();
            $output = '';
            while($archives->next()):
                $year_tmp = date('Y',$archives->created);
                $mon_tmp = date('n',$archives->created);

                $y=$year; $m=$mon;
                if ($year != $year_tmp) {
                    $year = $year_tmp;
                    $all[$year] = array();
                }

                if ($mon != $mon_tmp) {
                    $mon = $mon_tmp;
                    array_push($all[$year], $mon);
                    $output .= "<br><h3>$year 年 $mon 月</h3>";
                }
                $output .= '<div>['.date('m-d',$archives->created).'] <a href="'.$archives->permalink .'">'.$archives->title .'</a><!--('. $archives->commentsNum.')--></div>';
            endwhile;
            echo $output;
            ?>
        </section>
    </article>
    <?php $this->need('comments.php'); ?>
</div><!-- end #main-->

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

按下Ctrl+x并输入y后回车

2. 在 Typecho 后台启用

  1. 登录 Typecho 后台。
  2. 进入“管理” -> “页面” -> “新建页面”。
  3. 标题设为“归档”(或任意名称),自定义模板选择“文章归档”。
  4. 保存并发布,访问页面即可看到效果。

添加新评论

赞助商