Creating an Archive Index

跳转至: 导航搜索


So you want a single page with links to all your archived entries, arranged just so to form the main gateway into your blog's past. You can create template files to customize this archive gateway for each theme you use.

Or, maybe you're just wondering how to use the archives.php template file that's included with the WordPress Default Theme (wordpress 2.9 and before, the Twenty Ten theme does not include an archive template).

Creating the Archive Index Template and Page

To set up a separate archive index you'll need to create it as a Page, and assign it a special template.

The Template (archives.php)

Start off with a simple template called archives.php, stored in your theme's directory. The WordPress Default Theme includes such a template, and it makes a good starting point.

Note: the default theme for WordPress 3.0 and beyond does not have a built in archives template. Something like this should do the trick (you will need to adapt the html structure to match your current theme):

Template Name: Archives
get_header(); ?>

<div id="container">
	<div id="content" role="main">

		<?php the_post(); ?>
		<h1 class="entry-title"><?php the_title(); ?></h1>
		<?php get_search_form(); ?>
		<h2>Archives by Month:</h2>
			<?php wp_get_archives('type=monthly'); ?>
		<h2>Archives by Subject:</h2>
			 <?php wp_list_categories(); ?>

	</div><!-- #content -->
</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Technically, the template can be called almost anything (see these two lists for names you should not use; these are special file names WordPress reserves for specific purposes). However, using a standard name for your template will make it easier to change your blog's theme or distribute your theme and template to the WordPress community. It's also possible to display a large archive index using one of the all-purpose templates (like index.php or category.php) and the is_page function, but again, taking advantage of the theme system's modularity makes it easier for others (and for you!) to edit your template later.

For more information on creating templates, see Template Tags, particularly wp_get_archives.

The Archives Page

Create archives.php in your theme directory (wp-content/themes/themename/). Then from the Admin Panel, Pages > Add new

  1. Give your new archives Page a suitable title like Site Archives. Leave the Page content blank.

In the sidebar open the Page templates box, and select the Archives template. After saving it you will see a new item in your pages list, click on it, and enjoy!

Customizing Your Archives

List Archives By Year

To list your archives by year use the wp_get_archives template tag:

<?php wp_get_archives('type=yearly'); ?>