How can I theme the taxonomy/term/x page? [closed]
Although it was easy to find some info online about how to theme other stuff (e.g. search results), it is impossible to find a straightforward article about how to theme the output of a taxonomy/term/247 page?
How can I do it?
In Drupal 6, you can make use of node-taxonomy.tpl.php
and page-taxonomy-term.tpl.php
files in your theme to template taxonomy pages considering that the second one is the wrapper for the first. Behave node-taxonomy.tpl.php
like node.tpl.php
and page-taxonomy-term.tpl.php
like page.tpl.php
. for example:
page-taxonomy-term.tpl.php
<?php require 'header.tpl.php'; ?>
<body class="<?php echo $body_classes; ?>">
<div id="page">
<?php require 'page-navigation.tpl.php'; ?>
<div id="main">
<h2>Taxonomy term page</h2>
<div class="taxonomy-content">
<?php if ($tabs): echo '<div id="tabs-wrapper" class="clear-block">'; endif; ?>
<?php if ($title && !$node): echo '<h2'. ($tabs ? ' class="with-tabs"' : '') .'>'. $title .'</h2>'; endif; ?>
<?php if ($tabs): echo '<ul class="tabs primary">'. $tabs .'</ul></div>'; endif; ?>
<?php if ($tabs2): echo '<ul class="tabs secondary">'. $tabs2 .'</ul>'; endif; ?>
<?php if ($show_messages && $messages){ echo $messages; } ?>
<?php echo $help; ?>
<?php echo $content; // contains the output of node-taxonomy.php, that's why I call this wrapper template file. ?>
</div> <!-- #taxonomy-content -->
</div> <!-- #main -->
</div> <!-- #page -->
<?php echo $closure; ?>
</body>
<?php require 'page-footer.tpl.php'; ?>
node-taxonomy.tpl.php
<div id="node-<?php echo $node->nid; ?>" class="node<?php if($sticky) echo ' sticky'; ?><?php if(!$status) echo ' node-unpublished'; ?>">
<div class="taxonomy-node">
<div class="node-body">
<a class="node-title" href="<?php echo $node_url ?>" title="<?php echo $title ?>">
<?php echo $title ?>
</a>
<span class="node-cck-field">
<?php echo $node->field_cck_blah[0]['view']; ?>
</span>
</div>
</div>
</div>
Well, the most important part: By default the node-taxonomy.tpl.php
is not known to Drupal, so we need to introduce this as a template suggestion in the our theme's template.php
file, here we go:
/**
* Adding custom PHPTemplate suggestions on taxanomy pages.
*
* @param $vars
* A sequential array of variables to pass to theme template.
*/
function phptemplate_preprocess_node(&$vars) {
if(arg(0) == 'taxonomy'){
$suggestions = array('node-taxonomy');
$vars['template_files'] = array_merge($vars['template_files'], $suggestions);
}
}
Also there is a taxonomy-term.tpl.php
, regarding Drupal 7.
It's a code sample, dont' forget to use check_plain()
& check_url()
on printouts.
The taxonomy page is not magic in any that it requires something special to theme it. There's a template file, a preprocess function and some theming functions, much like any page.
If you would like to control the output of the default taxonomy page which is a bit raw, you can use views to overwrite the default page. You could then use views to only show node teasers, do some custom ordering, use a pager etc.
If you want to do something more specific you should edit your question to tell us what you want to do.
You better use following code in template.php
foreach ($vars['node']->taxonomy as $term) {
$vars['template_files'][] = 'node-term-'. $term->tid;
}
Because you can use file names such as node-term-YOUR-TERM-ID.tpl.php for each term .
if you want to make it more efficient use :
if ($hook == 'node') {
if (arg(0) == 'taxonomy') {
foreach ($vars['node']->taxonomy as $term) {
$vars['template_files'][] = 'node-term-'. $term->tid;
}
}
} //if bracket close
精彩评论