0

Clicking on a Taxonomy term generally takes you to a page showing several nodes which use that term. Is there a way to find out if you're viewing a specific vocabulary and save it to a variable? That way you can change the layout at will:

<?php if( $is_vocabulary_fruits ) : ?>
    Load this layout
<?php if( $is_vocabulary_cars ) : ?>
    Another layout different from the first
<?php else : ?>
    Nope, use this layout instead
<?php endif; ?>

I'm looking to place this in page.tpl.php. This means that the markup outside of the node.tpl.php is changed when viewing a taxonomy and not the node itself. How is this done in my template.php file?

2 Answers 2

2

When a taxonomy page is rendered, the template file used is taxonomy-term.tpl.php that receives, among other variables, the $term variable, which contains the term object for the taxonomy term associated to those nodes.
Basing on the vocabulary that owns that taxonomy term, you can change the layout of the page.

If you want to find out in page.tpl.php the taxonomy term associated with the nodes shown in the page (where the page's URL is example.com/taxonomy/term/[tid]), then you can use the following code:

$term = menu_get_object('taxonomy_term', 2);

If the page's URL is not example.com/taxonomy/term/[tid], then the function returns FALSE. The code can also be executed in a preprocess function, such as hook_preprocess_page().

2
  • This works but it still falls back to using the default page.tpl.php. I'm trying to look for a way so I can add extra markup to page.tpl.php by checking using the variables above which then loads the taxonomy-term.tpl.php file.
    – enchance
    Aug 1, 2011 at 20:11
  • 1
    The page.tpl.php file is always used for all the pages; it does not fall back on using it. First the template for the taxonomy term page is used, and then the template for the page is used. It is the same for a node; first node.tpl.php is used and then page.tpl.php is used.
    – apaderno
    Aug 1, 2011 at 21:04
1

I can think of a couple of ways to have different layouts based on vocabulary. One is through template suggestions: http://drupal.org/node/1089656. You would need to do your own template suggestion for your vocabularies and you can have different markup or classes.

Another way would be to add a vocabulary body class based on the taxonomy and you change the layout that way like:

.vocabulary-fruits #main {width:600px; float:right}

Look at: http://drupal.org/node/1072806

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.