I want to link taxonomy term with current loaded node I am using

$nid = arg(1);
print $term;

but noting happened. Please tell what is the correct way

  • please check this drupal.stackexchange.com/questions/8722/… – user21513 Feb 10 '14 at 9:46
  • Get the taxonomy ID - tid using db_select and node ID. And then load the taxonomy_term_load($tid), Which taxonomy field you want from the node? – inizio Feb 10 '14 at 9:54
  • i have differ kind of term and every term is linked with page for example term A has page 1 and its child term b has page 2 when i am click page 1 it go to page 1 not term A and same as when i am click page 3 it go to page 3 not to term b these data are shown in first side bar block all data are custom code – Adi Feb 10 '14 at 12:15
  • You want to say that you have 2 pages, PAGE 1 has associated with term A and PAGE 2 with term B(which is the child of term A). And when you click on PAGE 1 of course it will redirect to PAGE 1 only. Are you trying to redirect to term A? And when you click to PAGE 3, why it should redirect to term B? There is not association with PAGE 3. Can you explain it in more details? – inizio Feb 10 '14 at 13:23
  • oh sorry its no page 3 its page 2 – Adi Feb 10 '14 at 13:25
$nid = arg(1);
$node = node_load($nid);
//field_taxonomy is the taxonomy field used in the node
$tid = $node->field_taxonomy['und'][0]['tid'];
print $term;

In Drupal 6 and Drupal 7, taxonomy and node ids' are linked in term_node and taxonomy_index tables respectively. To programmatically link nid and tid, load the node and enter tid in it under hook_node_presave()

Create a new taxonomy object, if not already exist

$taxonomy = new stdClass();
$taxonomy->vid = xxx; // id of the vocabulary term
$taxonomy->name = 'abc'; // name of the term
$new_term = taxonomy_term_save($term);
$tid = $new_term->tid; // This will get you tid of new term 
                       // that is created or use taxonomy_term_load() if you have tid

Attach this tid in the node

$node->taxonomy_field_name_in_the_node['und'][]['tid'] = $tid;

submit this node.

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.