Yearly Archives

Posted by on Jan 10, 2010 in Articles, How-To | 0 comments

Place the following in functions.php to list the archives for a specific month and year:

function getarchives_filter($where, $args) {
	if (isset($args['year'])) {
		$where .= ' AND YEAR(post_date) = ' . intval($args['year']);
	if (isset($args['month'])) {
		$here .= ' AND MONTH(post_date) = ' . intval($args['month']);
	return $where;

add_filter('getarchives_where', 'getarchives_filter', 10, 2);

This is what you will add to your template file:

<?php wp_get_archives('type=daily&month='.get_the_time('m').'&year=' . get_the_time('Y')); ?>

If you’ve been blogging for a number of years, it can sometimes be helpful to list your archives by years, then by months, then by days.

If you use this method, your archive page will list every year you have a post in. Then when you click on a year, it will take you to a list of the months you posted in that year. Then when clicking on a month, you go to a list of days you posted.

If you want to use this method, add the following code to archive.php:

	<?php while (have_posts()) : the_post(); ?>
        <?php if ( is_year() ) : ?>
			<ul id="archives"><?php new_get_archives('type=monthly&year=' . get_the_time('Y')); ?></ul>
		<?php elseif ( is_month() ) : ?>
			<ul id="archives"><?php new_get_archives('type=daily&month='.get_the_time('m').'&year=' . get_the_time('Y').'&format=custom&before=&after='); ?></ul>
		<?php elseif ( is_day() ) : ?>
        	<div class="entry">
				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>

				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

				<?php edit_post_link('Edit this entry','','.'); ?>
		<?php else : ?>
		<div <?php post_class() ?>>
				<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
				<p><?php the_time('l, F jS, Y') ?></p>

				<div class="entry">
					<?php the_content() ?>

				<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>


	<?php endif; ?>

		<?php endwhile; ?>

Put this in archives.php:

<?php wp_get_archives('type=yearly&format=custom&before=|&after=|'); ?>

I also had to edit the get_archives function. I’m attaching a text file of that code because it’s a little long. To use, paste it into your functions.php (inside opening and closing PHP tags if they’re not already there). Be sure to also include the getarchives_filter function at the top of this page.

[Download not found]

Leave a Reply

Your email address will not be published. Required fields are marked *