MDV Archive

If you’re looking for a better way to archive in WordPress you’ve come to the right place.

In a nutshell, this is what MDV Archive does for you:

  • Disables paging for category, day, month, and search archives.
  • For day archives, an excerpt of each post for that day will be displayed.
  • For category, month, and search archives, the posts will be grouped by date. You will have permalinks to the post as well as to the comments for that post.
  • Year archives are disabled because there are just way too many posts in the course of a year to display on one page.

MDV Archive was built from the default archive.php shippped with WordPress 1.5. The modified archive.php contains a function to display the archive dates and it also throws some CSS into the HTML head in order to style the archive listings.

Installing MDV Archive is extemely easy. It only takes three steps:

  1. Copy the first section of code below. Create a file in your theme named search.php.
  2. Copy the second section of code below. Create a file in your theme named archive.php.
  3. The new archive.php might need to be modified to fit your theme. Sorry, but you are on your with that part.
<?php 
// Use the archive template instead of the search template
include(get_archive_template());
?>
<?php
function mdv_archive_date() {
/* 
MDV Archive
Author: Nick Momrik
Version: 1.1
Author URI: http://mtdewvirus.com/

This function takes care of displaying the date listings for the archives.
Placed here instead of in a plugin for ease of activation.
Simply drop the file in a theme.
*/

	global $post, $month, $mdv_previous_month, $mdv_previous_day;

	$thisday = get_the_time('j', false);
	$thismonth = get_the_time('m', false);
	$thisyear = get_the_time('Y', false);
	$output = '';
	
	if (!is_month() && $thisyear.$thismonth != $mdv_previous_month) {
		if ($mdv_previous_month) $output .= "</ul>\n</li>\n</ul>\n";
		$output .= '<br /><a href="' . get_month_link($thisyear, $thismonth) . '" title="View posts for ';
		$output .= $month[$thismonth] . ' ' . $thisyear . '">' . $month[$thismonth] . ' ' . $thisyear . "</a>\n";
		$output  .= '<ul class="arch-days">' . "\n";

		$mdv_previous_day = "";
	}
	elseif (is_month() && $thisyear.$thismonth != $mdv_previous_month) {
		$output .= '<ul class="arch-days">' . "\n";
	}

	if ($thisyear.$thismonth.$thisday != $mdv_previous_day) {
		if ($mdv_previous_day) $output .= "</ul>\n</li>\n";
		$output .= '<li class="arch-day"><a href="' . get_day_link($thisyear, $thismonth, $thisday);
		$output .= '" title="View posts for ' . $month[$thismonth] . ' ' . $thisday . ', ' . $thisyear . '">';
		$output .= $thisday . "</a>\n" . '<ul class="arch-posts">' . "\n";
	}

	$mdv_previous_month = $thisyear.$thismonth;
	$mdv_previous_day = $thisyear.$thismonth.$thisday;

	echo $output;
}

function mdv_arch_css()
	{
	if (is_archive()) {
	echo <<<END
	<style type="text/css" media="screen">
		/* Begin MDV-Archives CSS */
		ul.arch-days {
			margin: 0;
			padding: 0;
			list-style: none;
			}
		
		li.arch-day {
			margin: 0 0 0.7em 1em;
			}
		
		ul.arch-posts {
			margin: -1.0em 0 0 2.5em;
			padding: 0;
			position: relative;
			}
		
		li.arch-post {
			margin: 0 0 0.2em 0.8em;
			}
		/* End MD-Archives CSS */
	</style>
END;
	}
}

add_filter('wp_head', 'mdv_arch_css'); //Add CSS to head
?>

<?php get_header(); ?>

	<div id="content" class="narrowcolumn">

	<?php // Turns off paging for category, day, month, and search archives
		if (is_category() || is_day() || is_month() || is_search()) { 
			$posts = query_posts($query_string . '&nopaging=1');
		}
	?>
		
	<?php if (have_posts()) : ?>

		<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
		<?php /* If this is a category archive */ if (is_category()) { ?>
		<h2 class="pagetitle">Archive for the '<?php echo single_cat_title(); ?>' Category</h2>
		
		<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F jS, Y'); ?></h2>
		
		<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('F, Y'); ?></h2>

		<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
		<h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>
		
		<?php /* If this is a search */ } elseif (is_search()) { ?>
		<h2 class="pagetitle">Search Results</h2>
		
		<?php /* If this is an author archive */ } elseif (is_author()) { ?>
		<h2 class="pagetitle">Author Archive</h2>

		<?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
		<h2 class="pagetitle">Blog Archives</h2>

		<?php } ?>

		<?php if (is_year()) { ?>
			<p>Archives by year are not allowed. There are way too many posts in a year!</p>
		<?php } else { while (have_posts()) : the_post(); ?>
			<?php /* If this is a day archive, output post excerpts */ if (is_day()) { ?>
				<div class="post">
					<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(''); ?></a></h3>
					
					<div class="entry">
						<?php the_excerpt() ?>
					</div>
			
					<p class="postmetadata"><?php edit_post_link('Edit', '', '<strong>|</strong>'); ?>  <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?></p>

					<!--
					<?php trackback_rdf(); ?>
					-->
				</div>

			<?php /* All others use the MDV Archive */ } else { ?>
				<?php mdv_archive_date(); /* Output part of the date as needed */ ?>
				<li class="arch-post">
					<a href="<?php echo get_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
					<?php the_title() ?></a> (<?php comments_popup_link('0', '1', '%'); ?>)<?php edit_post_link("Edit", " (", ")"); ?>
				</li>
			<?php } ?>
		<?php endwhile; ?>

		<?php /* If category or month, output a few needed end tags */ if (is_category() || is_month()) { ?>
			</ul></li></ul>
		<?php } } ?>

	<?php else : ?>

		<h2 class="c">Not Found</h2>

	<?php endif; ?>
		
	<p><cite>Powered with 
	<a href="http://mtdewvirus.com/code/mdv-archive/" title="MDV Archive">MDV Archive</a> 
	by <a href="http://mtdewvirus.com/" title="Nick Momrik">Nick Momrik</a>.
	</cite></p>
	</div>
<?php get_footer(); ?>