Customising page layout using taxonomy terms
Last modified: September 8, 2006 - 17:13
description
This explains how to customise the entire page layout depending on taxonomy terms.
Step 1 of 2
- Make 2 copies of your page.tpl.php file and rename them page-termname.tpl.php and page-default.tpl.php
- using a text editor like notepad.exe or equivalent, edit your page.tpl.php and add the snippet below or variations of the same snippet.
- Upload your new page.tpl.php and custom page-temname.tpl.php layout files to your active theme folder and your new layout will take effect automatically
<?php
/**
* This snippet loads up a different page-termname.tpl.php layout
* file based on the taxonomy term of the node.
*
* For use in a page.tpl.php file.
*
* This works with Drupal 4.7
*/
/* load up the taxonomy terms for this node and page */
$terms = taxonomy_node_get_terms($node->nid);
rsort($terms);
/**
* Check to see if the taxonomy term matches your query and load
* a custom page-termname.tpl.php layout file
*/
if ($terms[0]->name == 'brochure') {include 'page-brochure.tpl.php';
return; }
/*If the taxonomy term name doesn't match, load the page-default.tpl.php */
else {include 'page-default.tpl.php';
return;}
?>adding more layouts/using variations of the same snippet
The example snippet above should be self explanatory and loads a custom page-brochure.tpl.php layout file when the taxonomy term name is 'brochure' .
If you prefer to refer to the taxonomy term ID, rather than the taxonomy term name, you can use the following snippet which looks for the $terms[0]->tid instead of the taxonomy term name.
<?php
if ($terms[0]->tid == '3') {include 'page-brochure.tpl.php'; /*load a custom page-brochure.tpl.php if the taxonomy term of the current node matches */
return; }
?>