One of the advantages of using WP Archives page is the amount of information that it can provide to help the visitors easily browse your blog and find what they’re looking for. It can also act a your blog’s sitemap which is perfect for search engines.
Most WordPress themes already have archives.php template file in your theme’s folder. In this episode of WordPress series we are presenting some basic and advanced techniques to create a more user-friendly and more appealing archives page.
You might e interested to check other posts in this WordPress Series:
- Mastering Your WordPress Theme Hacks and Techniques
- Most Desired WordPress Hacks: 11 Common Requests and Fixes
So lets get started, and don’t forget to subscribe to our RSS Feed as there is a small gift on its way.
Create Archives.php Template File
First, if you don’t have the archives.php file in your theme’s folder, create a new file and put the following code in it.
<?php /* Template Name: Archives Page */ ?> <?php get_header(); ?> <?php get_sidebar(); ?> <div id="content"> <h2 class="entry-title"><?php the_title() ?></h2> <?php the_content() ?> <h2>Archives by Month:</h2> <ul> <?php wp_get_archives('type=monthly'); ?> </ul> <h2>Archives by Subject:</h2> <ul> <?php wp_list_categories(); ?> </ul> </div> <?php get_footer(); ?>
After that you need to create a new page and change the “Page Template” option to the new “Archives Page” template file.
Planning your Archive
Probably the most prominent ways to display data on the Archives page is by using either one or two of the following concepts:
- Display all posts on the blog
- A monthly(or yearly) list
- Show all categories
1. Listing all Posts
1.1. Archives recent # of Posts
Displays archive list of the last twenty most recent posts listed by post title.
<?php wp_get_archives('type=postbypost&limit=20&format=custom'); ?>
1.1. Archives all Posts (Displaying Date, Category, number of comments, etc…)
We can create a more advanced archives page by using the loop. This will give us more flexibility to display all the info we need to show for each post listed (the published date, number of comments, catgory, and custom fields)
<?php while(have_posts()) : the_post(); ?> <h2><?php the_title(); ?></h2> <ul> <?php $totalposts = get_posts('numberposts=200&offset=0'); foreach($totalposts as $post) : ?> <li> <?php the_time('M j') ?> : <a href="<?php the_permalink(); ?>"><? php the_title(); ?></a> Posted in: <?php the_category(', ') ?> <?php comments_number('No Comments', 'One Comment', '% Comments' );?> </li> <? php endforeach; ?> </ul> <?php endwhile; ?>
1.2.WordPress Plugins to list all posts
WP Simple Sitemap is a WordPress plugin that automatically lists all your posts with a page number navigation. You can view a demo here.
This plugin lists the Month / Year, shows total post count for the month, the day of the month the article was published, the title of the article and the number of comments that have been made on each article. Also you can toggle on/off the moo.fx for collapsing months. You can view a demo here.
1.3. Smart WordPress Archives Solutions Listing all Posts
2. Display monthly or Yearly Archive
2.1. Template Tags/wp get archives
The wp get archives is a function that displays a date-based archives list. The parameter arguments are given to the function in query string format. This tag can be used anywhere within a template.
<?php wp_get_archives('arguments'); ?>
Displays archive list by month
<ul> <?php wp_get_archives('type=monthly'); ?> </ul>
Displays archive list by date, displaying only the last fifteen days.
<ul> <?php wp_get_archives('type=daily&limit=15'); ?> </ul>
Text to be placed after and before the link
Text to place before or after the link when using the html or custom for format option.
<?php wp_get_archives('before=«'); ?> <?php wp_get_archives('after=«'); ?>
Display number of posts in an archive
Display number of posts in an archive (1 – true) or do not (0 – false). For use with all type except ‘postbypost’.
<?php wp_get_archives('show_post_count=1'); ?>
<?php wp_get_archives(%u2019type=monthly&show_post_count=1%u2032) ?>
Dropdown Box Archive
Displays a dropdown box of Monthly archives, in select tags, with the post count displayed.
<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'> <option value=\"\">\ <?php echo attribute_escape(__('Select Month')); ?> </option> <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>
2.2. WordPress Archives Plugins (Sorted by month, year)
The WordPress Heat Map plugin provides you with three new template tags for displaying a heat map (aka weighted list) of your categories, tags and monthly archives.
Smart Archives is a rather simple WordPress plugin that will allow you to display your archives in a much “cleaner” format. Everything on the page is hyperlinked (years, months, posts). You can view a demo here.
3. Archives Ordered By Category
Display all the categories of your blog and show post count in each category.
<?php wp_list_cats('sort_column=name&optioncount=1') ?>
3.1. WordPress Archives Plugins (Ordered By Category)
This plugin displays a list of the latest post from each category of your WordPress installation. In the plugin’s options page you can choose the sort order, show or hide the dates, select the date formatting, show the first X characters of the content, display a link to the comments, and show comment counts, as well as other options.
This plugin implements a dynamic, AJAXified way of digging into the archives of a blog. You can check a live example here.
Further Readings on Creating your WordPress Archives
- Using custom fields to create archive thumbnails
- Grouped by Category– Grouping by category lays out Posts of same category under one heading, the category name. By using multiple loops.
- Creating an Archive Index