The Archive, Author, Category & Tags Template

Update: We’ve created a second edition of this article, with updated code samples and coverage of the latest theme development techniques. Check it out at The Archive Template. It’s part of The ThemeShaper WordPress Theme Tutorial: 2nd Edition.

Much like we did with index.php, we’re going to get one master template done right and use it to build our other templates. Our master Template in this case is the Archive Template.

What archive.php does (and all it’s related templates) is show posts based on a select criteria. A date range, or posts by a certain author, a category, or a tag. So, basically, it’s a lot like index.php. If you can read the name of the template you can figure out what it’s going to spit out.

Let’s start again with our template-template from the previous lessons and build on top of it.

<?php get_header(); ?>

		<div id="container">
			<div id="content">

				<div id="nav-above" class="navigation">
				</div><!-- #nav-above -->

				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
				</div><!-- #post-<?php the_ID(); ?> -->			

				<div id="nav-below" class="navigation">
				</div><!-- #nav-below -->					

			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

The Archive Template

Here’s the scheme of an Archive Template:

Call the_post()
Check to see what kind of template this is
Produce an appropriate template
Rewind the posts with rewind_posts()
Do the usual loopy WordPress stuff
Here’s the #content of your archive.php Template. Note the Conditional Tags at the top for checking to see what kind of template we’re in.

<?php the_post(); ?>			

<?php if ( is_day() ) : ?>
				<h1 class="page-title"><?php printf( __( 'Daily Archives: <span>%s</span>', 'your-theme' ), get_the_time(get_option('date_format')) ) ?></h1>
<?php elseif ( is_month() ) : ?>
				<h1 class="page-title"><?php printf( __( 'Monthly Archives: <span>%s</span>', 'your-theme' ), get_the_time('F Y') ) ?></h1>
<?php elseif ( is_year() ) : ?>
				<h1 class="page-title"><?php printf( __( 'Yearly Archives: <span>%s</span>', 'your-theme' ), get_the_time('Y') ) ?></h1>
<?php elseif ( isset($_GET['paged']) &amp;&amp; !empty($_GET['paged']) ) : ?>
				<h1 class="page-title"><?php _e( 'Blog Archives', 'your-theme' ) ?></h1>
<?php endif; ?>

<?php rewind_posts(); ?>

<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
				<div id="nav-above" class="navigation">
					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&amp;laquo;</span> Older posts', 'your-theme' )) ?></div>
					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&amp;raquo;</span>', 'your-theme' )) ?></div>
				</div><!-- #nav-above -->
<?php } ?>			

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

				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
					<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

					<div class="entry-meta">
						<span class="meta-prep meta-prep-author"><?php _e('By ', 'your-theme'); ?></span>
						<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>
						<span class="meta-sep"> | </span>
						<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your-theme'); ?></span>
						<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-dTH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>
						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class="meta-sep">|</span>ntttttt<span class="edit-link">", "</span>nttttt" ) ?>
					</div><!-- .entry-meta -->

					<div class="entry-summary">
<?php the_excerpt( __( 'Continue reading <span class="meta-nav">&amp;raquo;</span>', 'your-theme' )  ); ?>
					</div><!-- .entry-summary -->

					<div class="entry-utility">
						<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>
						<span class="meta-sep"> | </span>
						<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>ntttttt<span class="meta-sep">|</span>n" ) ?>
						<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span>
						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class="meta-sep">|</span>ntttttt<span class="edit-link">", "</span>ntttttn" ) ?>
					</div><!-- #entry-utility -->
				</div><!-- #post-<?php the_ID(); ?> -->

<?php endwhile; ?>			

<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>
				<div id="nav-below" class="navigation">
					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&amp;laquo;</span> Older posts', 'your-theme' )) ?></div>
					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&amp;raquo;</span>', 'your-theme' )) ?></div>
				</div><!-- #nav-below -->
<?php } ?>

The Author Template

Not a lot is going to change with our Author Template. You’re going to like this one. Copy archive.php and rename it author.php. All we need to change is the page title section.

				<h1 class="page-title author"><?php printf( __( 'Author Archives: <span class="vcard">%s</span>', 'your-theme' ), "<a class='url fn n' href='$authordata->user_url' title='$authordata->display_name' rel='me'>$authordata->display_name</a>" ) ?></h1>
				<?php $authordesc = $authordata->user_description; if ( !empty($authordesc) ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . $authordesc . '</div>' ); ?>

Easy, right?

The Category Template

The Category Template is another simple template now that we have a proper Archive Template. Copy archive.php and rename it category.php.

Now open up functions.php. We’re going to drop a custom function—from the brilliant Sandbox Theme—in there that’s going to make our Category Template a little more usable.

// For category lists on category archives: Returns other categories except the current one (redundant)
function cats_meow($glue) {
	$current_cat = single_cat_title( '', false );
	$separator = "n";
	$cats = explode( $separator, get_the_category_list($separator) );
	foreach ( $cats as $i => $str ) {
		if ( strstr( $str, ">$current_cat<" ) ) {
			unset($cats[$i]);
			break;
		}
	}
	if ( empty($cats) )
		return false;

	return trim(join( $glue, $cats ));
} // end cats_meow

Our custom function cats_meow() removes the current category from category pages. In other words, it gets rid of redundant categories in that list of categories we have just underneath the excerpt of our post.

Now, back in category.php, replace the page title section with the following code:

				<h1 class="page-title"><?php _e( 'Category Archives:', 'your-theme' ) ?> <span><?php single_cat_title() ?></span></span></h1>
				<?php $categorydesc = category_description(); if ( !empty($categorydesc) ) echo apply_filters( 'archive_meta', '<div class="archive-meta">' . $categorydesc . '</div>' ); ?>

And in the .entry-utility div, replace …

						<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>

with the modified …

<?php if ( $cats_meow = cats_meow(', ') ) : // Returns categories other than the one queried ?>
						<span class="cat-links"><?php printf( __( 'Also posted in %s', 'your-theme' ), $cats_meow ) ?></span>
						<span class="meta-sep"> | </span>
<?php endif ?>

The Tags Template

The Tags Template is almost identical to the Category Template, except, well, it’s for Tags. You know the drill: copy archive.php and rename it tag.php.

We’ve also got a custom function—again, from the brilliant Sandbox Theme—for our functions.php called tag_ur_it(). It works just like cats_meow() except it removes redundant tags.

// For tag lists on tag archives: Returns other tags except the current one (redundant)
function tag_ur_it($glue) {
	$current_tag = single_tag_title( '', '',  false );
	$separator = "n";
	$tags = explode( $separator, get_the_tag_list( "", "$separator", "" ) );
	foreach ( $tags as $i => $str ) {
		if ( strstr( $str, ">$current_tag<" ) ) {
			unset($tags[$i]);
			break;
		}
	}
	if ( empty($tags) )
		return false;

	return trim(join( $glue, $tags ));
} // end tag_ur_it

Now, in tag.php, replace your page title with:

				<h1 class="page-title"><?php _e( 'Tag Archives:', 'your-theme' ) ?> <span><?php single_tag_title() ?></span></h1>

and in .entry-utility, replace …

						<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>ntttttt<span class="meta-sep">|</span>n" ) ?>

with the modified …

<?php if ( $tag_ur_it = tag_ur_it(', ') ) : // Returns tags other than the one queried ?>
						<span class="tag-links"><?php printf( __( 'Also tagged %s', 'your-theme' ), $tag_ur_it ) ?></span>
<?php endif; ?>

And that’s it!

How To Create a WordPress Theme

This post is part of a WordPress Themes Tutorial that will show you how to create a powerful WordPress Theme from scratch. Read it from the beginning and code yourself up something awesome.

WordPress Theme Tutorial Introduction
Theme Development Tools
Creating a Theme HTML Structure
Template and Directory Structure
The Header Template
The Index Template
The Single Post, Post Attachment, & 404 Templates
The Comments Template
The Search Template & The Page Template
The Archive, Author, Category & Tags Template
The Sidebar Template
Reset-Rebuild Theme CSS & Define Your Layouts

15 responses

  1. […] the rest here: The Archive, Author, Category & Tags Template Tags: Comments0 Leave a Reply Click here to cancel […]

  2. Mikus Avatar

    Hello, I just wanted to know if it’s a good idea in terms of SEO to have the title of my site and the tagline displayed in both, the header of the site and the browser window.. I see that many professional sites like yours have a logo in the header and title+tagline in the browser window… However the themes comes by default with the text header.

    So can it harm my site in any way, to have the title and tagline two times? I mean, Search Engines will read my site’s title and tagline twice, because both appear in the header of the site and in the browser window.

    I hope you understand my question.

    Thanks.

    1. If you look at the source code for this site (and others, probably) you’ll see that my header does indeed use the title and tagline. The text is being replaced with an image using CSS.

  3. […] 原文:The Archive, Author, Category & Tags Template […]

  4. […] The Archive, Author, Category & Tags Template […]

  5. Should it be archives.php or archive.php? And don’t you need to declare a php file as a template in comments?

    1. Tiffany Avatar

      It should be archive.php

      Check out the php documents on google code:
      http://code.google.com/p/your-wordpress-theme/source/browse/trunk/

      It’s been very helpful for me.

  6. Hi, thanks for the good tutorial.

    I just wondered if it’s possible to configure the widgets as well?

    Right now I am hacking on the wp-includes/default-widgets.php to change the Meta plugin to show some of my special links instead of the default ones.

    The problem with this setup is that when I upgrade WP I might have to do it all again. Is there any way to define this in the theme?

  7. I understand the concept of these files but how do you NOT get an 404 error on the category directory itself, for example, http://www.mysite.com/blog/category/
    Without using a plug in of course (eg, Top Level Categories)
    and thanks for using sandbox in examples!

  8. I’m using wp-cumulus to display my tags which is very snazzy, unfortunately it keeps returning blank pages and I can’t seem. This also happens when I click a tag within the meta of the post itself, so I’m pretty certain that its not the plugin thats doing it.

    I think the error may be in the tag.php file itself but can’t figure out where. could someone take a look? http://pastebin.com/f6b7775d1

  9. just change the css code if you know you can change it. it is quite simple if you have an idea about css.

  10. Hey Stewart. Thanks for the base for the author template. I’ve been looking for days. I’ve implemented it just as you suggest and I’m pulling no content where the author info should be. I”m using the_author_posts_link() from the page.php within the loop. I’m using wp 2.9 and am wondering if there is a tweak needed for the new version.

  11. Hi there,

    I have catagorised posts pulling through to an archive page which is working correctly, but I want them to display fully just as the the original post looked, but they look quite different, can I make them appear in the Category archive fully as per the original post?

    Example:
    see first post on this page (Courtship Dating DJ Mix)
    http://soundcanvasmusic.co.uk/wordpress/

    I am using ‘Page links to’ Plugin to pull its category through to the ‘Mix archive’ page:
    http://www.soundcanvasmusic.co.uk/wordpress/?cat=8

    It pulls into the page, but want it to display the whole post, can this be done?

  12. […] The Archive, Author, Category & Tags Template […]