以前使用超简洁的WordStar主题,没有关心这个存档页面的问题,最近换了Kratos主题,百度无意间搜到掩耳的站点,参考了掩耳的博客菜单,但是发现文集这项,Kratos主题没有,但又不想使用插件,于是自建了模板页面.
1.如果使用子主题,在子主题文件夹下任意位置创建,否则直接在主题文件夹kratos-master下任意位置创建文件 archives.php ,文件名可根据自己习惯命名,该文件被识别为模板页是由页面顶部注释部分的Template name标示的.
2.文件内容如下
<?php
/**
* Kratos Cross actions file
*
* @category WordPress
* @package kratos
* @author Cxw (http://cxwstar.com/blog/)
*
* Template name: 存档页面
* Description: A archives page
*/
get_header();
$numberposts = '50';//显示的文章数,可修改
?>
<div class="k-main <?php echo kratos_option('top_select', 'banner'); ?>" style="background:<?php echo kratos_option('g_background', '#f5f5f5'); ?>">
<div class="container">
<div class="row">
<div class="col-lg-8 details">
<div class="article py-4">
<div class="header text-center">
<h1 class="title m-0"><?php the_title(); ?></h1>
</div>
<div class="container">
<p>此处仅显示最近的<?php echo $numberposts; ?>篇文章</p>
<div>
<ul>
<?php
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;
$myposts = get_posts('numberposts='.$numberposts.'&orderby=post_date&order=DESC');
foreach($myposts as $post) :
setup_postdata($post);
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
if($year != $previous_year || $month != $previous_month) :
if($ul_open == true) :
echo '</ul></li>';
endif;
echo '<li><p>'; echo $year.'年'.$month.'月'; echo '</p>';
echo '<ul class="archives-list">';
$ul_open = true;
endif;
$previous_year = $year; $previous_month = $month;
?>
<li>
<time><?php the_time('j'); ?>日</time>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a>
<span class="text-muted"><?php comments_number('', '1评论', '%评论'); ?></span>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
</div>
</div>
<div class="col-lg-4 sidebar d-none d-lg-block">
<?php dynamic_sidebar('sidebar_tool'); ?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
3,登录WordPress后台管理,新建页面,标题填入 文集 ,内容空着不填,页面属性模板选择存档页面,然后点击发布.
4,在菜单中增加页面文集到页头菜单并保存


