I have created a view (page, list of nodes) and have included the field "Taxonomy: Terms for Locations" (locations is the name of my vocab).
I am theming that View using the theme wizard.
The term is called to the template with <?php print $name?>
What I want to do is call $name twice - once for the node term, and once to get the most distant parent.
Heres what I mean - this is an example of my taxonomy tree;
Australia
- New South Wales
-- Blue Mountains
So if the nodes term is "Blue Mountains" I want to print "Australia" (the country).
I assume I will need to write a function in template.php and call it in the template (I suspect using taxonomy_get_parents_all).
Only problem is I really have no idea where to start, let alone how to grab the most distant parent and print it out.
Any help and guidance much appreciated, I am just learning Drupal and PHP so this stuff is a bit beyond me right now.
Comments
Do you know the vocabulary ID ?
If you have the vocabulary ID, then the following might be useful to find "Australia".
$adam_info = array_shift(taxonomy_get_tree($vid));
$adam = $adam_info->name;
Thanks for the reply. Yes, I
Thanks for the reply. Yes, I know the vid.
I see that array_shift will return the first element in the array - looks like smart thinking.
Hopefully I can make some ground on this today.
There might be some problem.
I later realized that the first element might not always be the distant element. e.g. in this tree, every item has the same vid:
- running array_shift(taxonomy_get_tree($vid)) on this will return the taxonomy object for "1". taxonomy_get_parents_all(), OTOH, will return only the parents of the given tid. The last (or the one before the last, not sure) taxonomy object in this returned array is the one you are looking for. So doing array_pop() once (or twice) on taxonomy_get_parents_all()'s returned array might be the ultimate solution. Let us know what happens.
Ok, using the first example,
Ok, using the first example, you are correct, indeed it gives me 1 (following you example above).
This the code I used;
I'm sure your suggesting to array_pop taxonomy_get_parents_all is correct, however, for the life I me I cant figure out how to put it all togeather. I'll keep working on it, any sugesstions would be most appreciated.
The problem I am having (as
The problem I am having (as far as I can tell) is getting the term ID from the variable $name, and then figuring out how to gets its parents.
Is that on the right track?
Re: The problem I am having (as
What's the value of $name ? Is it just a taxonomy term? If so then you can find the taxonomy object using taxonomy_get_term_by_name($name). This'll actually return all taxonomy term objects matching $name. Then you'll have to look into the "vid" field of each of the objects to find the right term object. Hope this helps.
Or ....
Or you might find it easier to query the database directly ...
This finds the parents of a term ....
Thanks John, might you
Thanks John, might you briefly explain how I would use that, do I put that in template.php and call it into the template, if so how?
Yes in template.php
Put it in template.php and just call it from page-xxx.tpl.php. Bear in mind that particular code was for Drupal 6.x so you'll just need to check that the database for taxonomy is the same in whatever version of Drupal you are using. I expect it is.
For instance in page-xxx.tpl.php
$call_function = $F1PHome_active_taxonomy($node->nid);
Great, thats get the right TID
Ok, I changed that to $call_function = F1PHome_active_taxonomy($node->nid); as I got the error "Function name must be a string...".
Anyway, that returns the correct TID (the most distant parents TID) - fantastic!
Sorry to be a bother, but how do I make it return the term name?
Would that perhaps be something to do with me using Drupal 5.x?
what you need to do is to
what you need to do is to query the database. Something like this should work:
$name = db_result(db_query("SELECT t.name FROM {term_data} t WHERE t.tid=$term");$term = the tid of the term you want the name of. You can then set return to $name.
This should work in D5
Fn call
Sorry the reason why the first code didn't work was because the "$" was a typo. Nothing to do with D5 vs 6
I'm loath to ask for more
I'm loath to ask for more help, however I don't really get it - not so sure what I am meant to change to get it to work.
Call from anywhere
You can call that fn from anywhere. If you want my F1PHome_xxx_yyy() to return the name of the taxonomy put that fn just before the return. Set the t.tid to $term_old in the SQL and that should get the name of the tid associated with $term_old. Then return that value.
eg,
Alternatively the F1Phome_xxx_yyy can return the number (tid) and you apply the fn in page-xxx.tpl.php file.
Great! Yes, now I understand
Great!
Yes, now I understand - works perfectly!
John & progga - thank-you so much for helping out with this and hanging in there with me:) I owe you both a cold one.
To anyone else stumbling on this - here is what I ended up with:
In tempate.php:
And in my templage (in my case views-list-xxx.tpl.php, but can be any template):
Great stuff!
Does this do the same thing?
$parents = taxonomy_get_parents_all($sourceTID);
$oldestParent = $parents[sizeof($parents)-1];
print $oldestParent->name;
Do we need to define the tid in that code?
Do we need to define the tid in that code? (like specify a number). Because what happens if you have lots of nodes and want to return the parent for each one? You can't specify each tid in the code, right?
Also, for Drupal 6, do we put this code in the template.php and call it in the theme file?
Simple Method
Yes @cinquetooty has the simple method listed above. Thanks!
where should it be added? as
where should it be added? as a function in template.php page? or as php in node-type template page?
Wherever you need it. Do you
Wherever you need it. Do you need it as part of a function override in template or just in a page display?
Thank you, that works
Thank you, that works great.
any idea how i can get now the children only?
this code used to work and
this code used to work and then since a few weeks ago gives this error message:
line 61 is
$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));[EDIT]
it seems that the error was caused by the fact that i was trying to use this code in a node type where taxonomy was chosen through a content_taxonomy field and not through standard taxonomy selector.
this code used to work and
> $parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = $term"));
"$term" is not defined. So you need to replace it with whatever the equivalent is. I am rewritten this line slightly:
$parent = db_result(db_query("SELECT h.parent FROM {term_data} d INNER JOIN {term_hierarchy} h ON d.tid = h.tid WHERE d.tid = %d", $term_id));
you're right, thank you
you're right, thank you