I have been struggling to put the parent term id of the node's term in views argument. Let me explain why am I trying to do such a weird thing. Well, on the term page I am showing a block with list of all the nodes under that term. But the moment one clicks at any node, that block disappears as the default argument (for term id) in the view is: if (arg(0) == 'taxonomy' && arg(2) != '') { return arg(2); } That is for the taxonomy arrangements like team>>Country>>Australia>> features, Articles etc. Here: vocabulary is Team: Country is main term, Australia is Child term and Feature, Articles etc. are child's children terms That is okay. But since I want to show the Block on that term's node pages also, I want to pull that node's term's parent term'ID [because node is (say) an article under Feature and the block of list of nodes I am showing is Under Australia term.] so that I could add further argument like: elseif(arg(0) == 'node') { then ...... Plz help.
0
votes
1 Answers
2
votes
If I understand the question correctly, you want to display a block that shows all the nodes with the same taxonomy term as the immediate parent of the node's term. In case the node has 2 terms a>b (that is a is b's parent), then the term is a. If you have a>b>c and all of them have been set, then you have a and b as some term parents. Then the block would have to show all nodes that have a and b as terms.
so the continuation would be:
else if (arg(0) == 'node' && is_numeric(arg(1)))) {
$n = node_load(arg(1));
$vid = 0; // change for the required vocabulary
$tids = array(); // will hold all the parents of the node's terms
foreach ($n->taxonomy as $tid => $term) {
if ($term->vid == $vid) {
$parents = taxonomy_get_parents($term->tid);
// the term has a parent
if (count($parents)) {
$parent = array_shift($parents);
$tids[] = $parent->tid;
// if you require only one parent term, return the first one that we find
// comment the next line if you want all terms that act as parents
return $parent->tid;
}
}
}
// in this case, make sure that you
// check the 'Allow multiple terms per argument' checkbox
// and argument type is 'Term IDs separated by , or +'
return implode(',', array_unique($tids));
}
In a way, the solution above works like the depth attribute and depth modifier for the term argument.