Bookmark and Share

The Archive Template Page

Saturday, October 10th, 2009 by MikeLT

While it may not seem like it on the face of things, the archive.php template page has some awesome responsibility. As the page of last resort for up to eight templates, the archive acts as the last chance for formatting before the index is called for the all important category, tag, author, and date pages.

For that reason, you can see that the PHP is a little more extensive, covering a multitude of ifs and elses’.

When building the archive page, I simply borrowed it from the default theme and made some minor tweaks. The default theme’s archive page pretty much covers the gamut for what should happen in case one of the many possible events occurs, so why reinvent the wheel?

Here is the Loop for my archive.php template page:

<!–WP-Loop for archive.php–>

<?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>Archive for the &#8216;<?php single_cat_title(); ?>&#8217; Category</h2>

<?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
<h2>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h2>

<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h2>Archive for <?php the_time(‘F jS, Y’); ?></h2>

<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h2>Archive for <?php the_time(‘F, Y’); ?></h2>

<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h2>Archive for <?php the_time(‘Y’); ?></h2>

<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h2>Author Archive</h2>

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

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class() ?>>

<h2 id=”post-<?php the_ID(); ?>”><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></h2>

<p><small>Posted on  <?php the_time(‘l, F jS,’) ?> by <?php the_author() ?> in Category <?php the_category(‘, ‘) ?> |   <?php comments_popup_link(‘No Comments &#187;’, ‘1 Comment &#187;’, ‘% Comments &#187;’); ?></small></p>

<p><?php the_excerpt(); ?></p>

<p><small><?php the_tags(‘Tags: ‘, ‘, ‘, ‘<br />’); ?></small> </p>

</div>

<?php endwhile; ?>

<?php else :

if ( is_category() ) { // If this is a category archive
printf(“<h2 class=’center’>Sorry, but there aren’t any posts in the %s category yet.</h2>”, single_cat_title(”,false));
}

else if ( is_date() ) { // If this is a date archive
echo(“<h2>Sorry, but there aren’t any posts with this date.</h2>”);
}

else if ( is_author() ) { // If this is a category archive
$userdata = get_userdatabylogin(get_query_var(‘author_name’));
printf(“<h2 class=’center’>Sorry, but there aren’t any posts by %s yet.</h2>”, $userdata->display_name);
}

else {
echo(“<h2 class=’center’>No posts found.</h2>”);
}
get_search_form();

endif;
?>

As you can see, even before the while loop is started, conditions are stated about what to do for all of the different archive types that can be called.

What to do if there are no posts used to just tell us nothing was there and offer a search box, now the default is to tell us what to print if other conditions cannot be met.

About all I did to customize this page was in the while loop. At first, I just wanted to show the title, tags, and post meta data, but after playing around some I added the excerpt. Then, I put the meta data under the title with small type, and did the same with the tags after the excerpt. It seemed to frame each post kind of nicely. Good enough for now.

October Archive Page

CSS Category Page Archive View

CSS Category Archive View

Bookmark and Share

Leave a Reply

You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Host Gator hosting service provider - unlimited domains