Configuring my WordPress Tag Template Page
Saturday, November 14th, 2009 by MikeLTTags associated with individual posts can be a powerful way of indexing a site. For me, I plan to write about multiple subjects, and hopefully will amass a good deal of content on each subject.
Tags are like categories on steroids, whereas a post will usually belong to one broad category, but can address a multitude of subjects (tags).
For example, in my previous post Optimizing the Header Template Page, I chose to place it in the SEO category because this particular template is where all of the imprtant meta tags that the search engines want to see are placed, such as title, description etc. But, the article also addresses the template hierarchy, the template pages themselves, even css and php on a broader scale, so I assigned each of these tags to the post.
Users can now click on a tag in the cloud of Sidebar1 and all posts that have been assigned that tag will show up on a page in chronological order.
Because I intend to write a lot of stuff on a lot of subjects, I want my tag page to be clean, so that I can quickly see all of my acquired knowledge on one particular subject. I don’t think I need to see the actual content, so I’ll just adjust the loop to show the title, some meta data, and the other tags the article is related to.
My Tag Page Loop
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>
<?php /* If this is a tag archive */ if( is_tag() ) { ?>
<h2 class=”pagetitle”>Posts Tagged ‘<?php single_tag_title(); ?>’</h2>
<?php } ?>
<?php while (have_posts()) : the_post(); ?>
<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 class=”postmetadata”><small>Posted on <?php the_time(‘l, F jS,’) ?> by <?php the_author() ?> in Category <?php the_category(‘, ‘) ?> | <?php comments_popup_link(‘No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></small></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;
?>
There are actually three levels of tag pages in the template hierarchy and WordPress will load them in the following priority:
- tag-slug.php
- tag-id.php
- tag.php
If there is no tag page, it will default first to the archive.php page, and the index.php as the page of last resort.
You can read more about the tag page in the template hierarchy at the WordPress Codec Tag Templates page.
Tags: hierarchy, tag page, tags, template page, the loop
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.



