I'm trying to theme all the taxonomy main pages, but I can't figure out how to theme just the content area of the taxonomy page. I edited node.tpl.php and that did exactly what I wanted, but that affects all nodes, not just taxonomy nodes. Using the path conventions on this page: http://drupal.org/node/104316 I thought I could just make a copy of node.tpl.php and rename it node-taxonomy.tpl.php and be off to the races, but no dice. I can create page-taxonomy.tpl.php, but that themes the entire page, not just the content area. I even tried page-node-taxonomy.tpl.php, page-node-taxonomy-term.tpl.php, node-taxonomy.tpl.php, node-taxonomy-term.tpl.php and none of them worked. I don't want to be specific on the term number because I want to theme all taxonomy main pages (i.e. that page that lists off all the nodes tagged with that taxonomy term). Any insight would be greatly appreciated.

Comments

IrnBru001’s picture

null

johnnyd’s picture

I used the following code in my template.php file to solve the problem:

if ($hook == 'node') {
   if (arg(0) == 'taxonomy') { 
      $vars['template_file'] = 'node-taxonomy'; 
   }
}

I then copied my node.tpl.php to a file named node.taxonomy.tpl.php, modified it to suit my needs, and put node.taxonomy.tpl.php in my theme directory.

Anonymous’s picture

I then copied my node.tpl.php to a file named node.taxonomy.tpl.php

I think that should read node-taxonomy.tpl.php -- at least that's what worked for me.

karlisson’s picture

If this don't changes anything, try putting the
return $vars;
after the second closing bracket.

cogat’s picture

This should go in the _phptemplate_variables function, like this:

function _phptemplate_variables($hook, $vars = array()) {
	print "Hook: ".$hook;
	switch ($hook) {
    case 'node':
   		if (arg(0)== 'taxonomy') {
			$vars['template_file'] = 'node-taxonomy';
		}
      break;
  }
 
  return $vars;
}
jessa_d’s picture

I extended this to the following:

function _phptemplate_variables($hook, $vars = array()) {

	switch ($hook) {
		case 'node':
			if (arg(0)== 'taxonomy') {
				$vars['template_files'] = array(
					'node-taxonomy',
					'node-'.$vars['node']->type.'-taxonomy'
				);
			}
		break;
	}

	return $vars;
}

Now, I can use node-taxonomy.tlp.php as the default template for a node on a taxonomy term page, and override that on a per-type basis in much that same way that drupal core lets you define node-[node type].tlp.php templates.

wescoughlin’s picture

override that on a per-type basis

How would you do this?

Say node-taxonomy.tlp.php is your defualt, and for a specific taxonomy term you wish to use a different node template, what would be the name. (i.e. would it be similar to node-taxonomy-[term name].tpl.php ?)

wescoughlin’s picture

I think I figured out what you ment....

I believe it would be node-[node type]-taxonomy.tpl.php which works perfect for me.

pbindagorge’s picture

I am using Drupal 6.x as a large article repository, so I wanted my taxonomy pages just to show the title and a custom CCK "link_comments" field (i.e., just a page with a list of links to the articles on my site, organized by taxonomy tags).

I read this thread and http://drupal.org/node/132442 (which documents the changes for Drupal 6), but I couldn't get it to work. So I found the devel module, which is AWESOME, and was quickly able to see what new page and node templates ("candidates") I needed to create to do this without having to write any PHP code in the template.php file.

For me this ended up being two new files: page-taxonomy-term.tpl.php and node-[your node type].tpl.php. My node type is "ocic_article", so my file was node-ocic_article.tpl.php. From there I was able to use devel module to see which variables were available to me in addition to $content.

I hope this helps someone!

Gerben Zaagsma’s picture

The above suggestions somehow don't work for me. When I put the code above in the template.php file, the new node-taxonomy.tpl.php template is not called.

If I do not modify the template file but follow the suggestion of pbindagorge the result is an empty page being displayed with only a taxonomy term.

It seems all pretty simple but cannot find out what goes wrong.

Any help appreciated,
Gerben

massimoi’s picture

Hi,
in my case it worked with page-taxonomy.tpl.php and not node-taxonomy.tpl.php

Hope it helps
Massimo
________________________________________________________
Massimoi :-] - http://impronta48.it - http://www.nkoni.org

OneTallShort’s picture

This code overrides the title and teaser for the taxonomy result pages with page_title and nodeword module fields. Keep in mind that as written, it doesn't reset the $node->title and $node->teaser variables, or the $node->content array, but that shouldn't cause many problems since MYTHEME_preprocess_node should be last out the door using $node content. If you want to make them match, just add some lines at the end to override those variables, eg $node->title = $vars['title']

 function MYTHEME_preprocess_node(&$vars, $hook) {
  $node = $vars['node'];
  if (arg(0) == 'taxonomy'){
    if (!empty($node->page_title)){
		$vars['title'] = check_plain($node->page_title);
	}
	if (!empty($node->nodewords['description'])){
     $vars['content'] =  $node->nodewords['description'];
	}
	else {
 	$vars['content'] = rtrim(strip_tags($node->teaser),150);
	}	
  }

}

Gregg Short
GShort.com, LLC Web Marketing and Design
gregg -at- gshort dot com

jeffschuler’s picture

In order to have node-taxonomy.tpl.php used for display of nodes on taxonomy term pages, I added the following function to template.php:

<?php
function phptemplate_preprocess_node(&$vars) {
  if (arg(0) == 'taxonomy') {
    $suggestions = array(
      'node-taxonomy'
    );
    $vars['template_files'] = array_merge($vars['template_files'], $suggestions);
  }
}
?>
unfeasible’s picture

You are officially my hero. Thank you so much for this.

youlikeicecream’s picture

This is DEFINITELY the right way to do it for Drupal 6

maxman’s picture

Hi, your solution works perfect and saves me lot of time. I modified it to support multiple content types.

<?php 
function [themename]_preprocess_node(&$vars) {
  if (arg(0) == 'taxonomy') {
    $suggestions1 = array(
      'node-taxonomy'
    );
    $suggestions2 = array(
      'node-'.$vars['type'].'-taxonomy'
    );
    $vars['template_files'] = array_merge($vars['template_files'], $suggestions1, $suggestions2);
  }
}
?>

For everyone who is not a programmer, you should insert the name of your template in the function. For garland the function would named: garland_preprocess_node. Then put the whole function into the template.php file in the directory of your template. If nothing happens try an cache-flush by clicking "flush all caches" in your "admin-menu".

If you dont want to do all this stuff, i have written a very basic module, which generates the template suggestion for you http://pieceofcode.rabbid.net/taxo_template_suggestion.zip

The module is based on jeffschuler`s snippet and you can use it by your own risk.

Again much much thanks to jeffschuler for posting your snippet :-)

Anonymous’s picture

Why it's duplicating the nodes

ltdung80’s picture

I need to build my website like your idea. But I can't . and I see your contribution very well. I want to clear your way to do.

your node.tpl.php file how to create ? may be, you can help me to see your pages. or help me to find your code.

my coding php language is not very well.

Thanks.

cascad’s picture

Please give me the code for 6.1.1

TapSkill’s picture

You don't need one!
D6 automatically reads the template files as far as I'm aware.
Just make sure your file is node-taxonomy.tpl.php.

---
I have created and maintained countless Drupal-powered sites and have made heavy modifications to modules on a site-by-site basis. I am an illustrator, a game developer, and a web developer. I also stream on Twitch in my spare time.

nedim.hadzimahmutovic’s picture

In my case, the problem was that I used path and pathauto modules to generate URL aliases. This helped in my case:

http://drupal.org/node/139766

after I added the code in template.php I created following tpl file:

page-termid.tpl.php

ressa’s picture

This example is using the Garland standard Drupal theme, I found the name for the template using the amazing Devel module.

1. Make a copy of page.tpl.php and rename it to page-taxonomy-term.tpl.php

2. Change line 70 to:
<?php if ($title): print '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>Nodes in the '. $title .' category</h2>'; endif; ?>

3. Flush the caches and check that the title now says "Nodes in the [TERM] category"

TikaL13’s picture

I just want the module to display my nodes tags like such:

Tag1 - Tag2 - Tag3

How can I get it to display like so?

anodenymous’s picture