I want to be able to call the current nodes taxonomy tree from page.tpl.php by using a function already in the taxonomy module. So far I've only been able to obtain the current nodes taxonomy id with:

$tid = $variables['node']->field_category['und'][0]['tid'];

I tried using: taxonomy_get_tree($tid);

but this only returns an empty array.

is there a way for me to use functions from other modules in _preprocess_page ?

full code:

function centro_preprocess_page(&$variables) {
     $tid = $variables['node']->field_category['und'][0]['tid'];
     $variables['taxTree'] = taxonomy_get_tree($tid);
  • You are indeed using the function successfully. I don't understand what you want to ask.
    – Jimmy Ko
    Jul 5 '16 at 16:37
  • Did you use print_r($result) to check the tree?
    – jmu
    Jul 5 '16 at 16:40
  • i used print_r(taxonomy_get_tree($tid)); but all i get is array(){}
    – blu
    Jul 5 '16 at 17:00

You are getting a taxonomy id. However, taxonomy_get_tree needs a vocabulary id as parameter, according to the documentation: https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_tree/7.x . That's probably why you are getting an empty array.

In order to be able to call taxonomy_get_tree, you would need to get the vocabulary id first:

$term = taxonomy_term_load($tid);
$vid = $term->vid;

(see this question for more details about getting the vid from a tid).

  • thanks this works. I'm guessing $vid is what distinguishes between tags and other types of custom taxonomy
    – blu
    Jul 5 '16 at 17:36
  • Yes, exactly. "Tags" (created by default when you install a Drupal site) is only one of the many vocabularies you can define. Each taxonomy term belongs to a vocabulary.
    – dinopmi
    Jul 5 '16 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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