Add Custom Text at the Top of Taxonomy Pages

The taxonomy module automatically generates a page for each taxonomy term that lists all the nodes tagged with that term. The URLs of these pages are in the format of http://example.com/taxonomy/term/.

By default, this page only shows the term name as the title and the teaser view of each node.

Sometimes you may want to display a short explanation or heading at the top of the page. This is possible by modifying the a theme to display the vocabulary description and/or term description. The code below adds both the vocabulary description (if it exists) and the term description (if it exists) to the top of each taxonomy page.

First, create some phptemplate variables in your theme template.php. (Keep in mind, you'll probably have more code in the _phptemplate_variables function, but this specifically highlights the code that is applicable to this solution.)


function _phptemplate_variables($hook, $vars = array()) {
switch ($hook) {
case 'page':
if (arg(0) == 'taxonomy') { //check to see if this is a taxonomy page
$vars['term_description'] = ''; //create a new template variable
$vars['vocab_description'] = ''; //create a new template variable
$a2 = arg(2); //get the term id
if(is_numeric($a2) && $a3 == '') {
$result = db_query(db_rewrite_sql("SELECT vid, description FROM {term_data} td WHERE td.tid = %d"), $a2);
$this_term = db_fetch_object($result);
//save the term description
$vars['term_description'] = $this_term->description;
$vocab_result = db_query(db_rewrite_sql("SELECT description FROM {vocabulary} WHERE vid = %d LIMIT 1"), $this_term->vid);
$this_vocab = db_fetch_object($vocab_result);
//save the vocabulary description
$vars['vocab_description'] = $this_vocab->description;
}
}
break;
}
return $vars;
}

Then, create a new template file in your theme by copying page.tpl.php and naming the copy 'page-taxonomy.tpl.php'. Note: Drupal 5 will automatically recognize template files with this naming convention.

In your new template file, add the following lines to call and display the term description (probably above print $content):

<?php if (!empty($vocab_description)) { ?>
<?php print $vocab_description; ?>
<?php } ?>
<?php if (!empty($term_description)) { ?>
<?php print $term_description; ?>
<?php } ?>

Note: you only need to print the $term_description variable. The HTML surrounding it is optional.

I also added this as a handbook page on Drupal. http://drupal.org/node/191959