Hi everyone, i can't figure out how to print a taxonomy term on a drupal page.tpl file.
I'm not a php developer and there's something missing in my code (and in my mind), in addiction to the fact that i can't find solution working for D7.
That's my scenario:
i'd like to have a page that shows:
- The taxonomy term
- The title
- The body
(optionally, additional fields, no problems for that)

what i try (better saying one of the stuff i tried ;-)

I create a vocabulary "projects" and term "education", then a field "category" (as taxonomy field) in the content-type.
before the title tag i put in my page.tpl the code:

<?php
print render($content['field_projects']); 
?>

then i try to write a function in my template.tpl file by useing the taxonomy_load_term as successor of taxonomy_get_term, but, as i said, i'm bad in php:

function mytheme_preprocess_page(&$variables) {
 if(arg(0) == 'taxonomy' && arg(1) == 'term') {
 $tid = (int)arg(2);
 $term = taxonomy_term_load($tid);
 if(is_object($term)) {
 $variables['category'][] = 'page__projects__'.$term->vocabulary_machine_name;
 }
 }
 }

This has no effects on my page.
what i'm missing?
Thank you very very much in advance
Alberto

Comments

f4o’s picture

I suggest to use $node variable which is available in page.tpl.php. This variable has taxonomy term reference field you want to put above title.

Let's say your content type is "dummy" and its field with taxonomy term reference is "field_category".

template.tpl.php

function mytheme_preprocess_page(&$vars) { // replace mytheme with your theme name
  if (isset($vars['node']->type)) {
    $vars['theme_hook_suggestions'][] = 'page__' . $vars['node']->type;
  } 
}

page--dummy.tpl.php (before title tag)

<?php if($node->field_category): ?>
<div class = "top_tag">
  <?php print $node->field_category['und'][0]['taxonomy_term']->name;?>
</div>
<?php endif;?>
tiikeri’s picture

Thank you very much for the help f4o,
but your code look like a Drupal6 function.
As i recognized the D6 coding i changed $vars by $variables, but maybe it's not sufficient.
Something else look to be changed in the function and maybe in the hook too, but i'm not sure.
I will try again soon, gotta be away for a couple of days, anyway many thanks ;)
in the meanwhile any help will wellcome

f4o’s picture

It's D7 code. Have tested it there. The name of the argument variable is up to you. You can call it $blah, if you like and it will work. :)
(Though it is better to stick with documentation standards.)

tiikeri’s picture

I confirm that the code above works very fine.
Many thanks Franko.
Hope this topic could helps someone else too.

tiikeri’s picture

The taxonomy term is printed as expected, thanks to the help of f4o, anyway i occasionally found those errors when surfing from another node to the page-progetti.tpl node's:
Notice: Undefined variable: node in include() (line 45 .... page--progetti.tpl.php).
Notice: Trying to get property of non-object in include() (line 45 .... page--progetti.tpl.php).

the line above correspond to if($node->field_category):
anyway we have to consider the line print $node->field_category['und'][0]['taxonomy_term']->name;

i read a lots of posts and documentation about similar issues and i found that the error should be generated by the ['und'] element which stands for "undefined"
i read also about isset (which verify, if i'm not wrong) if an element is set to null [0] or not.

As i mentioned before, i'm not a php developer, so i'm trying to understand what's happening with that code. If i refresh/reload the page, the error is not shown anymore.

I read several topics in which this is a common error while trying to print a field in the theme, solutions found around (for someone works ['#items'] instead of ['und'], for someone else add $node->language) doesn't works for me.

Thanks a lot for any help ;-)

designcrs’s picture

When checking arrays "empty" needs to be used instead of "isset". The following line should work:
if(!empty($node->field_category)):