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

pobster’s picture

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
--------------------------------------------

tdimg’s picture

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:

print $node->taxonomy[1]->tid;
scottrigby’s picture

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

omnyx’s picture

to me at least - because this is exactly what I was looking for.

thanks!

hanskuiters’s picture

To add more value to this post, I like a solution to a simular problem.

Part of my $node is this:

[field_country] => Array
        (
            [0] => Array
                (
                    [436] => stdClass Object
                        (
                            [tid] => 436
                            [vid] => 5
                            [name] => Czech Republic
                            [description] => 
                            [weight] => 0
                            [language] => en
                            [trid] => 0
                        )

                    [view] => Czech Republic <br />
                )

        )

In the node.tpl.php $node->field_country[0][436]->name gets 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?

yan’s picture

Have you tried $node->field_country[0]['view']?

hanskuiters’s picture

Oops, am I that blind? I guess I am. Thank you very much.

yan’s picture

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:


  $country = array();
  foreach ($node->field_country[0] as $i){
    array_push($country, $i->name);
  }
  $my_country_name = $country[0];
franckweb’s picture

After getting the taxonomy term(s) how can I get its alias??

Id like to print it as a link.

pobster’s picture

Pass it through either url() or l() (see http://api.drupal.org) like this;

print url("taxonomy/term/". $term_id);

Pobster

franckweb’s picture

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?

pobster’s picture

DId you even try what I suggested? The url function passes it through Drupal and hence, gives you the aliased link.

Pobster

franckweb’s picture

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 :

<?php
          foreach ($node->taxonomy as $taxonomia) { 
            if ($taxonomia->vid=="1")/*this is the taxonomy id*/
              echo "<li><a href=\"".$base_url.url("taxonomy/term/".$taxonomia->tid)."\">Taxonomy ".$taxonomia->name."</a></li>";
          }
?>
pobster’s picture

Couple of things to note;

  • You don't need the $base_url - the url() function takes care of that for you
  • If you're creating links then it's better to use the l() function e.g. echo "<li>". l(t('Taxonomy @name', array('@name' => $taxonomia->name)), "taxonomy/term/$taxonomia->tid") ."</li>";
  • If you're creating lists of things, then rather it's better to use theme('item_list', $list); this way your Drupal theme can handle the css properly

Pobster