Drupal: show taxonomy term description on page

Drupal

Edit: Drupal 7 displays the taxonomy description on taxonomy pages, no need to tweak templates. However, you can still use this post to add a class to the page title on taxonomy pages..

I wanted to display the taxonomy term description on taxonomy pages like site.com/taxonomy/term/n (where n is the term ID).

It actually took me quite a while but I figured it out. You can use the function taxonomy_get_term(n) where n represents the term ID.

In page.tpl.php I added the following code just before the print $content statement:

<?php /* Add taxonomy description if page is taxonomy page */ ?>
<?php $page_term = taxonomy_get_term(arg(2)); // path: site.com/taxonomy/term/n ?>
<?php if (arg(0) == 'taxonomy' && $page_term) { ?>
  <div class="taxonomy-term-info">
    <?php print $page_term->description; ?>
  </div>
<?php } ?>

The arguments are numbered from 0 up, so for the URL site.com/taxonomy/term/n arg(2) will return the value n for the taxonomy term ID. Then the function taxonomy_get_term(arg(2)) will return the term data, stored in $page_term in the above example.

We can then simply print the description in page.tpl.php with <?php print $page_term->description; ?>.

I added a test to check arg(0) otherwise it may cause problems for pages like site.com/aggregator/categories/n. This was also added to the page title so I could add a new class to the header tag:

<h2
  <?php
   
if (arg(0) == 'taxonomy') {
      print
' class="taxonomy-title"';
    }
 
?>

  > //don't forget the h2 closing bracket
  <?php print $title; ?>
</h2>

The page title will then normally be in <h2>..</h2> tags and <h2 class="taxonomy">...</h2> when we are on a taxonomy page. In the style sheet you can then add something like this:

h2.taxonomy-title {
  /* add your style properties here */
}
.taxonomy-term-info {
  /* add your style properties here */
}

You can see the result using a link from the tag cloud on the right.

For arg() it does not matter whether you are using aliases or not — the arguments for taxonomy and aggregator pages remain the same for Drupal.

Next I will add some tests to see if we retrieved a single term ID or more. In that case, I should move the code to a function in template.php and call it from page.tpl.php.

Another method: override taxonomy

The above example works. However, if you prefer an override of the taxonomy module, you can use the following. In template.php you can add code to override the taxonomy function to display the taxonomy term page.

<?php
function MYTHEME_taxonomy_term_page($tids, $result) {

 
// Add CSS from taxonomy module
 
drupal_add_css(drupal_get_path('module', 'taxonomy') .'/taxonomy.css');

 
// Display description for single term only
 
if (count($tids) == 1) {
   
$term = taxonomy_get_term($tids[0]);
   
$description = $term->description;

   
// Check that description exists
   
if (!empty($description)) {
     
$output = '<div class="taxonomy-description">';
     
$output .= filter_xss_admin($description);
     
$output .= '</div>';
    }
  }

 
$output .= taxonomy_render_nodes($result);

  return
$output;
}
?>

This code overrides the taxonomy description on the taxonomy page. You may also want to override the title for the page.

The part in the page.tpl.php template that adds the class to the header is pretty much the same as in the earlier example, except it has been polished a bit.

<h2<?php print (arg(0) == 'taxonomy') ? ' class="taxonomy-title"' : '';?>>
  <?php print $title; ?>
</h2>

This second method keeps the template file a little cleaner.

And talking about keeping templates clean, you may as well use page-taxonomy-term.tpl.php so we do not need to check if we're on a taxonomy page.

Comments

hi.

i wanna put adsense on any term page. how i can do it? where is the code? ic couldnt find.

when a visitor visits a term page like below:
http://www.MY_SITE.net/index.php?q=taxonomy/term/24

there will be adsense there...

thanks..
(please e-mail me, if you'll help me)

You could simply use:

<?php
if (arg(0) == 'taxonomy') { ?>

  <!-- add your code here -->
<?php } ?>

in your page template to test whether you're on a taxonomy page and then add your code (or move the code to the template.php as in the post). You can test for other arguments than 0 as well.

However, Drupal also has the adsense module: http://drupal.org/project/adsense — this might be a problem because you don't seem to be using clean URLs.

it works! thanks a lot..

I was thinking about this, and it may be cleaner to use another template than the standard page.tpl.php:

page-taxonomy-term.tpl.php

or

page-taxonomy-term-x.tpl.php where "x" is the term ID.

This way, you don't need to check whether you're on a taxonomy page.

Great article, helped a lot to easily solve an irksome problem.

I did it in a page-taxonomy-term.tpl.php to avoid the checks, and it worked great.

Thanks for contributing your experience.

Thanks.

This post was insanely helpful. Thanks for taking the time to put this information up(and keep it for so long).

-Chris

Glad it was helpful.

This is super helpful! Thank you very much!

You're welcome.