By yan on
Every now and then I face the same problem when I work on my themes. There is the $node variable which contains a lot of information about the actual node. I use to print it using
print_r($node);
But then: How do I get the variable I want to? I can, for example call the body content (and just text) which after printing $node is displayed as follows:
[content] => Array
(
[body] => Array
(
[#weight] => 0
[#value] => Whatever
[#printed] => 1
)
)by typing:
$node->content['body']['#value'];
But I can't, for example, do the same thing for the taxonomy id (tid). In $node it's present as follows:
[taxonomy] => Array
(
[1] => stdClass Object
(
[tid] => 1
[vid] => 1
[name] => Whatever
[description] => Whatever
[weight] => 0
)
)So I try the following to get the tid:
$node->taxonomy[1]['tid'];
This doesn't work, nor does any of the following:
$node->taxonomy[0]['tid'];
$node->taxonomy['tid'];
$taxonomy[1]['tid'];
$taxonomy[0]['tid'];
Can anybody tell me how I can call all the variables in $nodes? What am I doing wrong?
Comments
Try this; print
Try this;
print $node->taxonomy[1]->tid;It's an object you're trying to reference, not an array.
Pobster
--------------------------------------------
http://www.justgiving.com/paulmaddern
--------------------------------------------
difference between object and array
The structure is slightly confusing, I know. This always depends on whether you are dealing with an object or an arrays you call a direct 'child' of an object by $object->child, but a direct child of an array by using $array['child']. This now gets slightly more complicated in your case, as $node is an object, taxonomy an array but the items of taxonomy are objects:
Thanks so much! I've been
Thanks so much! I've been looking everywhere for this -- hope to return the (indirect) favor someday... (if only to someone else)
Scott Rigby
http://basekamp.com
http://PlausibleArtworlds.org
Scott Rigby
http://basekamp.com
http://PlausibleArtworlds.org
http://UtopiaSchool.org
and you did return the favor
to me at least - because this is exactly what I was looking for.
thanks!
To add more value to this
To add more value to this post, I like a solution to a simular problem.
Part of my $node is this:
In the node.tpl.php
$node->field_country[0][436]->namegets me the country name 'Czech Republic'.But 436 is related toe the country name, and for every node that number is different.
What should I set in stead of 436 to make it work for all nodes?
A try
Have you tried
$node->field_country[0]['view']?Oops, am I that blind? I
Oops, am I that blind? I guess I am. Thank you very much.
No problem.
No problem. Actually I've had a similar problem where, if I remember correctly, there was no "view" item in the array. So if anybody has an idea on how to solve it, help welcome.
One way to do it might be a foreach statement (untested), but it doesn't look very nice to me:
what about the link?
After getting the taxonomy term(s) how can I get its alias??
Id like to print it as a link.
Pass it through either url()
Pass it through either url() or l() (see http://api.drupal.org) like this;
print url("taxonomy/term/". $term_id);Pobster
Alias to get the link
That shows the url with the ID I dont want that ... I want the url with the alias.
For example, I have:
[taxonomy] => Array
(
[52550] => stdClass Object
(
[tid] => 52550
[vid] => 8
[name] => Science Fiction
[description] =>
[weight] => 0
)
)
I'd like to print:
<a href="mysite.com/category/science_fiction">Science Fiction</a>I already added the alias with "Automated alias settings". How can I get the alias to build this link?
DId you even try what I
DId you even try what I suggested? The url function passes it through Drupal and hence, gives you the aliased link.
Pobster
simple code to show any taxonomy as links
I was passing the wrong id. It works great! Thank you. This is the simple code to show any taxonomy as links by keeping the ALIAS url in the href parameter of :
Couple of things to
Couple of things to note;
echo "<li>". l(t('Taxonomy @name', array('@name' => $taxonomia->name)), "taxonomy/term/$taxonomia->tid") ."</li>";theme('item_list', $list);this way your Drupal theme can handle the css properlyPobster