这篇文章上次修改于 414 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
本方法不需要使用归档相关插件,仅需一段代码即可为你的typecho添加文章归档页面:
1.新建一个php文件,可任意命名
2.复制以下代码到php文件中:
<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?>
<?php
/**
* archives
*
* @package custom
*/
$this->need('header.php'); ?>
<div id="mainbox2">
<div class="post" id="post-<?php $this->cid(); ?>">
<h1><span class="post-title"><a href="<?php $this->permalink() ?>" title=""><?php $this->title() ?></a></span></h1>
<div class="clear"></div>
<div class="entry">
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
$year=0; $mon=0; $i=0; $j=0;
$output = '<div class="post-content cf">';
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>'. $year .' 年</h3><ul>';
}
if ($mon != $mon_tmp) {
$mon = $mon_tmp;
$output .= '<li><span>'. $mon .' 月</span><ul>';
}
$output .= '<li>'.date('d日: ',$archives->created).'<a href="'.$archives->permalink .'">'. $archives->title .'</a> ('. $archives->commentsNum.')</li>';
endwhile;
$output .= '</ul></li></ul></div>';
echo $output;
?> <div class="clear"></div>
</div>
</div>
</div>
<?php $this->need('footer.php'); ?>
3.保存并上传到当前typecho主题目录下
4.在控制台——管理——独立页面中新建一个页面,自定义模板选择archives,保存发布即可。
备注:
根据不同typecho主题的不同,你可以将此代码插入到主题所给出的二次开发模板中,以完成更好的适配。
主要代码:
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
$year=0; $mon=0; $i=0; $j=0;
$output = '<div class="post-content cf">';
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>'. $year .' 年</h3><ul>';
}
if ($mon != $mon_tmp) {
$mon = $mon_tmp;
$output .= '<li><span>'. $mon .' 月</span><ul>';
}
$output .= '<li>'.date('d日: ',$archives->created).'<a href="'.$archives->permalink .'">'. $archives->title .'</a> ('. $archives->commentsNum.')</li>';
endwhile;
$output .= '</ul></li></ul></div>';
echo $output;
或以下代码皆可:
<?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10000')->to($archives);
$year=0; $mon=0; $i=0; $j=0;
$output = '<div class="wrapper">';
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 .= '</div></div>';
if ($year != $year_tmp && $year > 0) $output .= '</div>';
if ($year != $year_tmp) {
$year = $year_tmp;
$output .= '<h1>'. $year .' 年</h1><div>'; //输出年份
}
if ($mon != $mon_tmp) {
$mon = $mon_tmp;
$output .= '<div><div><h4>'. $mon .' 月</h4><ul>'; //输出月份
}
$output .= '<li>['. $mon. date('.d] ',$archives->created).'<a href="'.$archives->permalink .'">'. $archives->title .'</a> </li>'; //输出文章日期和标题
endwhile;
$output .= '</ul></div></div></div></div>';
echo $output;
?>
有关如何创建自定义模板,请参考typecho的官方文档:点击这里
演示效果:
没有评论