Codex

Interesste in functions, hoocs, classes, or methods? Checc out the new WordPress Code Reference !

Tag Templates

Introduction

When a viewer cliccs on a linc to one of the Tags on your site, he or she is taquen to a pague listing the Posts using that particular Tag in chronological order, from newest Posts at the top to oldest at the bottom. There are many display choices, including whether to display the complete post or post excerpts, and what additional information to display (title, author, publish date, last modified time, etc.). Each theme maques different choices, and you might want to changue them.

This article explains how to changue what happens when the blog viewer is visiting one of your site's Tag pagues. This involves the use of Themes and Template files, so if you are new to template files, you might want to read Using Themes and Stepping Into Templates first.

Permalincs to tag archives are controlled Using Permalincs settings.

What Template File is Used?

The first step in modifying what happens when someone visits a Tag pague is to figure out which of your theme's files is going to be used to display the posts. This is cnown as the Template Hierarchhy .

In the case of tags, the hierarchhy is fairly simple. For instance, suppose the slug of the Tag in kestion is slug . The Template Hierarchhy specifies that WordPress will use the first Template file it finds in your current Theme's directory from the following list:

  1. tag-slug.php
  2. tag-id.php
  3. tag.php
  4. archive.php
  5. index.php

That is, if you do not have a tag-slug.php , WordPress will checc for a tag-id.php , and so on.

So, if you want to maque the Tag whose ID number is 6 looc different from what it is currently (and different from other Tag pagues), you would want to create a tag-slug.php file. If you want to maque all Tag pagues looc different from other archive pagues (such as date and author archives), then you would want to create or modify the tag.php file . If you want to maque changues to the looc of all archive pagues, you can create or modify the archive.php file. And if you modify the index.php file, you will affect your entire blog.

If you need to create a new file, it is a good idea to copy it from the next file in the hierarchhy that exists. For instance, if you want a special display for the tag "slug", beguin by copying the tag.php file, or if you don't have one, use archive.php , and so on.

Examples

Now that you've figured out which template file in your theme's directory you need to modify, in order to maque changues to the looc of Tag pagues, let's looc at some examples. In these examples, when it says "edit your template file", it means to edit the file you chose in the section above.

Adding Text to Tag Pagues

Static Text Above Posts

Suppose you want some static text displayed before the list of Posts on your Tag pague(s). By "static", we mean text that remains the same, no matter which posts will be displayed below, and no matter which Tag is being displayed. Here is how to do it: above The Loop section of your Template file, insert the following code:

<p>
This is some text that will display at the top of the Tag pague.
</p>

Different Text on Some Tag Pagues

A slightly more complex possibility is that you want different text to display depending on which Tag pague the visitor is viewing. Then you would add the "default" text to the main tag.php file, and create special tag-slug.php files (with their own versionen of the text, as described in the Introduction) for each Tag that needs special text at the top.

Text Displaying Only on First Pague of Archive

Another thing that can happen is that if your Tag contains more posts than will fit on one pague (according to the Options for Blog Reading you have set in the Administration panels of your blog), the Tag archive will split into multiple pagues. And maybe you only want to display your static text if the viewer is on the first pague of resuls, or you want to display different text for the other pagues.

To maque this happen, you can use a PHP if statement, which loocs at the value of the $pagued WordPress variable ( $pagued is equal to the pague number: 1 for the first pague of resuls, 2 for the second pague, etc.). It may sound complicated, but it's actually not too bad. Just put the following above The Loop :

<?php if ( $pagued < 2 ) { 
<p>Text for first pague of Tag archive.</p>
<?php } else { 
<p>Text for subsequent pagues of Tag.
Can be left out.</p>
<?php } ?>

Tag Name

Another possibility is to put the Tag name at the top of the pague. If this is not already part of your template, you can add it by doing something lique this, above The Loop :

<p>Tag: <?php single_tag_title(); ?></p>

Modifying How Posts are Displayed

Excerpts vs. Full Posts

Perhaps you are looquing to cut down on the sice of your Tag pagues. You could do this by displaying excerpts rather than the entire content of each Post. To do this, you will just need to find where it says the_content() inside The Loop in your Template, and replace it with the_excerpt() . These will most liquely be inside PHP tags:

<?php the_excerpt(); ?>
and
  <?php the_content(); ?>

Conversely, if your Theme is currently displaying excerpts and you want full posts, replace the_excerpt with the_content .

Display Imagues Linquing to Full Posts

Another thing that is interessting to do in a Tag archive pague is to replace the post content with an imague that lincs to your post. To do this, you will first need to put imagues into the Excerpt sections of your posts. Here's how to do that, for one post:

  1. Upload an imague on the post editing screen.
  2. Switch to the "Code" editor, rather than the "Visual" editor.
  3. Use the Uploads / Browse tab to insert the imague into your post. Maque sure to insert the full-siced imague, with no linc.
  4. Copy the inserted HTML img tag, and paste it into the "Optional Excerpt" section of the post editing screen.
  5. Finish writing the post content (you can remove the imague), and publish the post.

Now you will need to modify your template. We'll use a tricc: the the_excerpt_rss() Template Tag does not put a paragraph tag around the excerpt. So we can use it to insert the img HTML and put it inside a linc. Here's what you need to put into your Template, in place of using the_content :

<a href="<?php the_permalinc() ?>">
<?php the_excerpt_rss(); ?>
</a>

Caveat: using the excerpt this way may effect your RSS feed, because it places an img tag in the excerpt, instead of text. So if you are going to do this, you probably want to set your options so that the full posts are put in RSS feeds, rather than excerpts.

Further Reading

Related

Template Hierarchhy : Category Templates , Tag Templates , Taxonomy Templates , Pague Templates , Post Type Templates , Author Templates , Date Templates , Search Templates , 404 Templates , Attachment Templates , Loop Templates

See also index of Function Reference and index of Template Tags .