January 11, 2009

How to theme the heading title of a taxonomy page in Drupal

Filed under: Tips — Tags: , — Webopius @ 1:12 pm

In Drupal 6, when you navigate to a page listing all nodes for a specific taxonomy term (e.g. /taxonomy/term/1), Drupal will render a page for that term with the term description as the title heading and a list of nodes matching the term below this. If no nodes match the term then the text “There are currently no posts in this category.” is shown.

Styling this page is pretty straightforward using CSS and by editing the node.tpl.php template for your theme.

The big challenge though is that the page heading code looks something like this:

<div class="taxonomy-term-description">Term Description here</div>

For SEO purposes, as this is the key description for the page, you might want to wrap this in an H1 tag like this:

<h1>Term Description here</h1>

This is where you might start to hit problems. I have to be honest, I spent about 2 hours searching Google for a solution without much success. Most people seems to suggest either changing the core taxonomy module code or using the views module.

In my case, I like to have full control over the rendering of pages so I tend not to use the views module much and I wanted a cleaner solution than changing the Drupal core.

Themed Taxonomy Title – the solution

So, I dug deeper and the solution is actually very simple once you figure out how the theme() function hierarchy works. For my research I have to thank the excellent Pro Drupal Development 2nd Edition by John K. VanDyk. This book is a lot clearer to follow than some of the online Drupal API pages.

The key was within the taxonomy module in the file ‘taxonomy.pages.inc’. Within this file is a function taxonomy_term_page. This function makes a call to theme() as follows:

$output = theme('taxonomy_term_page', $tids, taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE));

If you look later in the same file, you’ll see the definition theme_taxonomy_term_page() itself.

Now, the important issue here is that when theme(‘taxonomy_term_page’,…) is called it looks in the following places for a match (let’s assume the theme you are using is called ‘mytheme’ and you are using the usual phptemplate theme engine):

  1. mytheme_taxonomy_term_page()
  2. phptemplate_taxonomy_term_page()
  3. sites/all/themes/custom/mytheme/taxonomy_term_page.tpl.php
  4. theme_taxonomy_term_page()

Because by default there are no matches for 1 to 3 above, the standard taxonomy theme_taxonomy_term_page() is called.

Having understood this concept, the solution is to write your own taxonomy_term_page() function within your theme’s template.php file (create this file if it doesn’t exist). An example of this is shown below, make sure you change the start of the function name to match your theme’s actual name:


function yourthemename_taxonomy_term_page($tids, $result) {
  $output = '';

  // Only display the description if we have a single term...
  if (count($tids) == 1) {
   $term = taxonomy_get_term($tids[0]);
   $description = $term->description;
   // Check that a description is set.
   if (!empty($description)) {
     $output .= '<h1>';
     $output .= filter_xss_admin($description) ;
     $output .= '</h1>';
    }
  }
  $output .= taxonomy_render_nodes($result);
  return $output;
}

Now, when you view a taxonomy term page, the title should be wrapped in <h1> tags


Add to Technorati Favorites

11 Comments »

  1. Hi! Thanks for your suggestion. But, what about writing that description on the meta-description tag instead?

    Comment by FiNeX — March 23, 2009 @ 5:18 pm

  2. [...] a recent article I wrote describing how to change and theme the format of the taxonomy pages in Drupal, FiNeX asked how to also get this title into your pages meta description. So, here’s how you [...]

    Pingback by How to change a page meta description and keywords in Drupal 6 ~ Webopius - Web design | Project Management | SEO | Graphic Design | E-Commerce | Content Management — March 23, 2009 @ 7:32 pm

  3. Hi FiNeX,
    Thanks for the comment. I’ve written an article for you that I hope will explain how to do this. Here’s the URL:

    http://www.webopius.com/content/211/how-to-change-a-page-meta-description-and-keywords-in-drupal-6

    Let me know if this makes sense and good luck with your site.

    Comment by Webopius — March 23, 2009 @ 7:36 pm

  4. hi,

    thanks for this information. but I need to make my taxonomy pages list the nodes, not with the teaser, just the node title.
    Of can i do it?

    Comment by jose — April 25, 2009 @ 7:20 pm

  5. Hi Jose,

    That’s be in your theme’s node.tpl file, you need to create something along the lines of this:

    <?php if ($page) { ?>
    [ Include your normal node page layout here...]
    <?php } else { ?>
    <div class="node<?php if ($sticky) { print " sticky"; } ?><?php if (!$status) { print " node-unpublished"; } ?>">
    <?php if ($title) { ?><h2><a href="<?php print $node_url? rel="nofollow">"><?php print $title?></a></h2><?php }; ?>
    </div>
    <?php } ?>

    Comment by Webopius — April 25, 2009 @ 8:53 pm

  6. I came accross this page whie googling for a solution to my problem…
    This page is so close, I wonder if anyone can tell me a way to simply not display the taxonomy term in the published page at all.
    For the site I am using, for most visitors seeing the term at the top of a piece of content is not neccessarry.
    I use drupal6.x with tac_lite and views, cck, devel…
    Any help appreciated. Sorry if this seems off-topc or stupid, but this page is the closest I have found to a solution so please point me to the right place if I am in the wrong place.

    :)

    Comment by Tony — May 2, 2009 @ 5:03 pm

  7. Hi Tony,

    To not display any term at the top of the page, try creating this function in your theme’s template.php file:

    function YOURTHEMENAME_taxonomy_term_page($tids, $result) {
    return taxonomy_render_nodes($result);
    }

    That should do what you need.

    Comment by Webopius — May 2, 2009 @ 6:09 pm

  8. Thanks for the reply,
    I must say it is a joy to look under the hood of Drupal and get to grips with how it works, not like a lot of the other code I have to deal with…. — thanks for the help, it has got me working on some useful template suggestions. :)
    I just found a module that does exactly what I want with the taxonomy terms.
    http://drupal.org/project/taxonomy_hide
    Thanks again!

    Comment by Tony — May 3, 2009 @ 12:51 am

  9. Thanks for your suggestion, I’ll try to follow your hints!

    Comment by FiNeX — June 22, 2009 @ 2:05 pm

  10. another idea is here (views are not that bad…)

    http://www.drupalove.com/drupal-video/crazy-awesome-taxonomy-pages

    Comment by manuel — January 4, 2010 @ 2:54 pm

  11. This is probably the most popular CAPTCHA module used in drupal (the creator of Drupal had a large part in making this one). If set up to do so, it will first filter comments and posts for sketchy content and then only run a CAPTCHA test if it deems necessary. It’s extremely powerful and configurable. It also has full-featured reports and and integrates with the Mollom servers to continously grow a database of sketchy behaviour and blacklistees.

    Comment by العاب — April 29, 2010 @ 2:39 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment