How to make a WordPress archive page

So, you want an archive page: a single page like an index or site map for your blog, with all your individual post titles in a list, as well as links to monthly entries and category entries - click "blog archive" up there at the top if you want to see any example of what I mean. Archive pages can make it easy for readers to see at a glance how much your blog has to offer; they're also great to present to search engines of course.

Iron Age Ikea
Creative Commons License photo credit: mrmanc

If your theme doesn't ship with such a page already installed, you might turn to the WordPress Codex to find out how to make one. And frankly, you'd probably end up as confused as I am trying to figure out just what the Codex is trying to tell me.

The problem is - we're back to WordPress's dodgy jargon here - that "archive" is used to mean two entirely different things. There's the site map-like page above, and there are also the search result-like lists of posts for dates (montly or yearly), categories and authors. This latter set of archives is usually generated by a file called archive.php. So if you're going to have the other kind of archive page as well, it's vital that you call it something other than archive.php. The WordPress default theme has an archive page template, and calls it archives.php. Frankly, I find this a bit too confusing and call my own version archive-page.php.

The easiest way to make your archive page is to copy the page template that's already in your theme (almost certainly page.php). Save a copy of page.php as archive-page.php and upload it to your WP theme (so you can easily edit it through WP admin).

Editing the archive page

Insert this bit of code at the very beginning of your archive page file (deleting any similar label if it's already there): <?php
/*
Template Name: Archive
*/
?>
This ensures that the file is properly labelled in your Appearance Editor, and you won't get it mixed up with the other kind of archive!

Next, remove the code that generates the normal page file: almost certainly this is everything from <?php if (have_posts()) : ?> down to the final <?php endif; ?>

Now you're ready to add the code that generates the archive links.

Archives by date

Use the function<?php wp_get_archives(); ?>By default, this will generate a list of monthly archives, wrapped in <li> tags.

  • To change the date ranges, add type=daily/weekly/monthly/yearly.
  • To limit the number of links generated (e.g. only the last 20 months) add limit=xx where xx is a number.
  • To show the number of posts for each month, add show_post_count=true.

For example: <?php wp_get_archives('type=daily&limit=14'); ?>would generate daily archives for the last two weeks.

Archives by category

Use <?php wp_list_categories(); ?> The default usage, showing all parent and child categories, is exactly what you want for the archives page.

All post titles

As for the date archives: <?php wp_get_archives('type=postbypost') ?>Again, use limit=xx to only show the most recent xx number of posts. This automatically generates a <li>-bounded list of titles linking to the appropriate single post pages.

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: , , , , ,

Posted by Sue on May 5, 2009 in WordPress.

16 comments to "How to make a WordPress archive page"

  1. Wayne Sharer (@hmigroupllc) wrote:

    This is a great post, and I just made a page like you said.

    But one thing -- How do I now display the page????

    It doesn't just show up somewhere. What do I do??

    Thanks

    Wayne

  2. Sue (@blogmum) wrote:

    Wayne: Create a new page, give it a title "Archives" or similar, **select the archive template** for it, and publish. You don't need to give it any actual page content. Then it will show up in any list of pages you've got on your blog.

    • Mike Carter wrote:

      Sue, the only choices in the dropdown Template box are:
      Archive
      Default
      Custom Search Page

      I called my new archive-page.php per your suggestion, but that doesn't show up as a Template choice. Am I to delete the original Archive.php? THAT would mess up the other archive areas I have for posts wouldn't it?

      Mike

  3. Thanks Sue. I have to admit, that I am impatient when it comes to my blog. I eventually found this tab on my "new page" and got it set up before you had a chance to reply.

    But thank you very much for taking the time to reply. Your blog is a great resource. I am glad I found it.

    This month is my "Free traffic resource month". Though your not directly a traffic resource, I emphasize using WP blogs. I think I will have to highlight your blog sometime during the month.

    This is a really great blog you made!

    Wayne Sharer

  4. John wrote:

    Is there away way to create archives for only one category instead of archiving posts for all your categories?

    • Sue (@blogmum) wrote:

      Hi John,

      Yes, there is... depending on exactly what you're trying to do.

      Normal category archives
      are generated by default. If you want to create a list of them, wp_list_categories() will do that for you,
      or if your theme has a widgetised sidebar, there's an inbuilt widget to create a list of category archive links.

      Changing the layout for specific categories
      If you want one particular category archive to have its own special layout, then you can do that too. You need to make an extra theme file called category-xx.php where xx is the number of the category you want the archive to be for.

      (Find the category ID by navigating to Admin > Posts > Categories and then hovering over the link to the relevant category; in the browser status bar, you'll see ....cat_ID=xx - and that's the number you want.)

      That would give you a specific page for one category; you can do this for just one category, or a few, or all of them. Any categories which don't have their own individual page like this will use the default archive page from your theme.

      The Codex page on template hierarchy will explain a little bit more about how this works, but do give me a shout or leave another comment if you need more help.

  5. Amr wrote:

    This is a wonderful and really useful tutorial. it is pretty clear and simple and was so helpful for me as a beginner in the WordPress world. thank you for this and keep going.

  6. sterndal (@sterndal) wrote:

    hello ma'am

    great post

    it's very easy to understand!

    i just noticed that commenters in your blog have the option for twitter id

    how did you do that?

    thanks!

  7. Eric wrote:

    Thanks for summing this up, Sue. If I'm doing my archives by post name with postbypost, is there a way to have each post's date listed after its title? Thanks in advance!

  8. Thank you! I used this for my blog's archive page, and it's working beautifully. :)

  9. Stephen wrote:

    Sue,
    Ordinarily, when you choose a category it takes you to an archive page with all the post content under that category.

    I want my categories to be landing pages, with an index of links to posts under that category. That way i can funnel all the link juice to my category landing pages which will be moneterised.

    I would also like to edit thoes pages with the visual editor.

    Is this possible.
    thank you
    Stephen

  10. Mike Carter wrote:

    REALLY great, easy to understand explanation. BUT, what I'm trying to set up is a page for my Pages, not Posts. I can't quite figure how or what to substitute in your code. Can this even be done?

    My goal is to keep the listing off the home page.

    Thanks, and, yes, I'm a novice at WordPress.

    Mike

Tweetbacks

Leave a Reply