许多博友喜欢为自己的博客建立一个文章归档页面,就如新浪博客的博文归档一样。WordPress 可以用插件来实现,但我们一贯遵守可以不用插件则不用的准则,现在就为大家带来免插件仅代码实现文章归档的方法。
新建一个为archives.php文件
<?php
/*
Template Name: 文章归档
*/
?>
<div class="archives">
<?php
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;
$myposts = get_posts('numberposts=-1&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 '</table>';
endif;
echo '<h3>'; echo the_time('F Y'); echo '</h3>';
echo '<table>';
$ul_open = true;
endif;
$previous_year = $year; $previous_month = $month;
?>
<tr>
<td width="40" style="text-align:right;"><?php the_time('j'); ?>日</td>
<td width="400"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td>
<td width="120"><a class="comm" href="<?php comments_link(); ?>" title="查看 <?php the_title(); ?> 的评论"><?php comments_number('0', '1', '%'); ?>人评论</a></td>
<td width="120"><span class="view"><?php if(function_exists('the_views')) the_views(); ?>次浏览</span></td>
</tr>
<?php endforeach; ?>
</table>
</div>
在主题目录下的style.css中进入以下代码:
.archives td{padding: 6px 10px 8px;border-bottom: solid 1px #eee}
.archives table{padding:10px 0 20px}
.meta-tit{border-bottom: solid 1px #e6e6e6;padding: 0 0 10px;margin-bottom: 20px}
然后在wordpress后台,页面-新建页面,题目任取,模板选择“存档”就可以实现了!

