I need to theme Taxonomy pages on a client site and am finding out that D7 uses the node.tpl for its rendering of taxonomy pages.

How can I target either all and/or specific taxonomy pages for theming? There is a template in the core Taxonomy module, but that only controls the description of the term.

Is there a way to target Taxonomy pages via overriding tpl files?

Thanks!

Comments

brunitto’s picture

Hi there, I shared the same issue. I just created a portfolio using Taxonomy and Content Types on Drupal 7. I noticed that there is a template file called "taxonomy-term.tpl.php" in the "modules/taxonomy" directory, copied it to my custom theme directory but I could not discover when it is used - you just said that it's used for describing a term, thanks.

Over that, I am afraid of having to use Views module (afraid to use such powerfull and complex module for a trivial task), if I need to change the ordering rule for Taxonomy items - rendering alphabetically instead od publish date.

Any help?

mouloud’s picture

Same issue here, and so far, Google didn't get me an answer... I will continue looking, though.

ryivhnn’s picture

Views isn't that hard. It's probably way overkill for a simple theming problem but if you want to reorder you would either need to use Views or a template.php override which would be requiring sql queries. I fund Views quicker and easier personally :)

If you're bringing in new tpl.php don't forget to clear the cache or they won't pick up.

works at bekandloz | plays at technonaturalist

benarobinson’s picture

Drupal templating in general leaves a lot to be desired.

Here's one relatively clean way to do it. In my case, I thought it would be foolish to write template files that would be attached to a term's arbitrary numeric ID, because if you're actually planning to work in dev before making changes to a production site, your taxonomy terms may have different IDs in each place (since Drupal just hands them out at random.) For that reason, it makes no sense to name them things like: 'taxonomy-term--vocabulary--2345.tpl.php', and it makes more sense to be able to name them things like 'taxonomy-term--vocabulary--term-name.tpl.php'. To make that possible, since terms don't have machine names by default, I had to install this module: https://www.drupal.org/project/taxonomy_machine_name

Then you can do this:

function THEME_preprocess_taxonomy_term(&$variables) {
  $term = $variables['machine_name'];
  $vocabulary = $variables['vocabulary_machine_name'];
  $variables['theme_hook_suggestions'][] = 'taxonomy_term__'.$vocabulary.'__'.$term;
}

If your vocabulary machine name is 'category' and your term machine name is 'cool_stuff', it'll now look for a template named:

taxonomy-term--category--cool-stuff.tpl.php

mcanada’s picture

I get a value on $vocabulary but nothing for $term (NULL). Any ideas why?

LEternity’s picture

subscribe

eurekaloop’s picture

subscribing.

LEternity’s picture

This seems to be related to http://drupal.org/node/948964. I posted a solution there.

drupal_was_my_past’s picture

subscribe

feo’s picture

There is a code snippet for Your template.php to do this :

<?php
function YOURTEMPLATE_preprocess_page(&$vars) {
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = arg(2);
    $vid = db_query("SELECT vid FROM {taxonomy_term_data} WHERE tid = :tid", array(':tid' => $tid))->fetchField();
    $variables['theme_hook_suggestions'][] = 'page__vocabulary__' . $vid;
  }
}
?>

I was inspired by this post for Drupal 6.
http://adaptivethemes.com/page-template-suggestions-for-taxonomy-vocabs

marvix’s picture

any one tried this code?
did not worked for me !

turmis’s picture

Worked for me.

You have to create files like page--vocabulary--3.tpl.php

Thanks feo ;)

sharikov’s picture

You need to replace the function argument "&$vars" with "&$variables

function YOURTEMPLATE_preprocess_page(&$variables) {
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = arg(2);
    $vid = db_query("SELECT vid FROM {taxonomy_term_data} WHERE tid = :tid", array(':tid' => $tid))->fetchField();
    $variables['theme_hook_suggestions'][] = 'page__vocabulary__' . $vid;
  }
}
Anonymous’s picture

I want to theme with different vocabulary node not vocabulary page

ytsurk’s picture

nice one

aha - interesting ..

heneryh’s picture

I've spent the whole afternoon googling this topic and have gotten nowhere. I fancy myself pretty computer literate and nearly an expert in programming languages such as java, C++ etc but I must be a total moron with this topic.

All I see are thousands of replies showing two or three lines of snippets to a pre-process function.

I see no reference or linkage to any themes or css style comments. How does the code above end up changing anything in the theme?

Could someone help me find a tutorial for morons on this?

I have a custom theme based off the zen theme. I would like to customize the banner, logo and maybe some minor other styles based on where the viewer is in the taxonomy.

I found the zen css that sets these items but how do I switch based on taxonomy.

rachelf’s picture

Depending on what you want to achieve, you might just be able to alter the display of the teaser for a particular content type.

The taxonomy page displays the nodes in a teaser view. Go to the 'Manage display' settings for your content type, click on the Teaser tab, and you can select which fields you wish to display.

(Of course, if you're after changing the order of display, then you are going to need to delve into the template.php or views solutions described above).

andyhu’s picture

You may also try this module: http://drupal.org/project/tvi
Using views to override the default taxonomy page.
Thanks!

giannina’s picture

Subscribing.

HonoredMule’s picture

It's certainly aggravating that there are no theme hooks for lists of things. There really ought to be a theme_preprocess_nodelist function hook and equivalent theme_nodelist[--source[--type]] template hooks. Just like there ought to be built in pages for listing contents of a given content-type (and I believe the two holes in extensibility are closely related). Views is powerful, but it's incredibly overpowered for so many simple things that should be achievable without any contrib modules at all. It would be nice to avoid that configuration-driven beast once in a while. Luckily, sometimes you can.

You can cheat your way around the lack of node listing (for taxonomy pages, at least) using HOOK_preprocess_block() like so:

function HOOK_preprocess_block(&$vars) {
	
	// add support for uniquely theming taxonomy term pages (the whole content, not just the lousy term fields)
	$term = @$vars['elements']['term_heading']['term'];
	if(@$term['#theme'] == 'taxonomy_term') {
		$vid = $term['#term']->vid;
		$vname = $term['#term']->vocabulary_machine_name;
		$vars['theme_hook_suggestions'][] = 'block__taxonomy_term__' . $vname;
		$vars['theme_hook_suggestions'][] = 'block__taxonomy_term__' . $vid;
		$vars['theme_hook_suggestions'][] = 'block__taxonomy_term';
	}
}

Then create your block template like so:


// this template is used when theming the list of nodes in a taxonomy term page
// and depends on block theme override in HOOK_preprocess_block() for taxonomy term pages

// this will use the appropriate template for the taxonomy term display
print render($variables['elements']['term_heading']);

// this will render the teasers the same as before, but now within our grasp
// equivalent to print render($variables['elements']['nodes']); 
foreach($variables['elements']['nodes'] as $key => $node) {
	if(is_numeric($key))
		print render($node);
}

// we need the pager too
print render($variables['elements']['pager']);

Now you can do all the nice things you'd expect full theming control to enable like adding the pager to the top of the page, putting html between nodes, or creating special node views that output a table row or a list item, etc. (by altering the #view_mode property) and wrap that output in appropriate html.

jibran’s picture

function hook_menu_alter(&$items) {
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'my_taxonomy_term_page';
}
function my_taxonomy_term_page($term) {
  $build = taxonomy_term_page($term);
//do you magic here
  return $build;
}
sujoyd1’s picture

Hello,
Please tell me how I can get this $build value in "page--taxonomy.tpl.php".

x7ian’s picture

That code should go in a module, not a template.

anjjriit’s picture

Are you sure ?
return Fatal error: Call to undefined function taxonomy_term_page()
for me.

x7ian’s picture

taxonomy_term_page() its a posible custom function that must return the contents that you wixh for your taxonomy page to have.

anou’s picture

You can do it with this module : Taxonomy display

David THOMAS
http://www.smol.org