1

I'm following another question I asked, How can I get the parent term in a taxonomy with a hierarchy? and I can get the parent term when I am on the view operation, but not when I'm on the insert operation. var_dump() shows the $terms variable, but NULL for $pterms. Here is my code being called in hook_nodeapi('insert').

  foreach ($node->taxonomy as $term) {
    $terms[] = $term;
  }
  $tid = $terms[0]->tid;
  $parents = taxonomy_get_parents($tid);
  foreach ($parents as $term) {
    $pterms[] = $term;
  }
  • Are you sure that the term already exists at the time when you run this? What does dpm($terms) tell you? Does it show you complete term objects, or just stub objects with only a few properties set? – donquixote Nov 28 '13 at 21:19
2

Did you try taxonomy_get_parents_all()?

$parents = taxonomy_get_parents_all($tid);
  foreach ($parents as $term) {
    $pterms[] = $term->name;
  }
| improve this answer | |
  • taxonomy_get_parents_all() calls taxonomy_get_parents(). In which cases would taxonomy_get_parents_all() work, but taxonomy_get_parents()` would not work? – kiamlaluno Aug 30 '11 at 22:56
  • unless you want to find all the ancestors you don't need taxonomy_get_parents_all(). I suggested this as in the previous question it's mentioned there are several parents. – zarsiwala Aug 30 '11 at 23:55
  • I ended up using tokens and the termpath token, but this looks like it would work for getting the exact parent term. – Arosboro Aug 31 '11 at 19:17
0
function Yourthemename_breadcrumb($variables)
{
  // init
    $breadcrumb = $variables['breadcrumb'];

    // taxonomy hierarchy
    $hierarchy = array();
    if (arg(0) == 'gallery' && arg(1) == 'term' && is_numeric(arg(2))) 
    {
        $tid = (int)arg(2);
        $parents = taxonomy_get_parents_all($tid); 
        $parents = array_reverse($parents);
        $breadcrumb = array();
        $breadcrumb[] = l('Gallery', '/');
         foreach( $parents as $k=>$v)
        { 
            $breadcrumb[] = l($v->name, 'taxonomy/term/'. $v->tid);;
        }
    }


    // rendering
    if (!empty($breadcrumb))
    {
        $output = '<h2 class="element-invisible">' . t('You are here') . '</h2>';
        $output .= '<div class="breadcrumb"><ul><li>' . implode("<span>></span><li>", $breadcrumb) . '</li></ul></div>';
        return $output;
    }

} 
| improve this answer | |
  • Hello. This code might be a solution, but OP is asking why. So, some explanation would be a good idea. If your code works, it may solve his problem but he will stay ignorant about how things works. If your code is generally good but some minor issue will cause it to fail in OPs environment, your answer is totally useless. So please add some human explanations, not just code, OK? :) – Mołot Dec 6 '13 at 9:30

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.