Geeks and God Podcast

(206)984-3694geeksandgod@gmail.comfeeds.feedburner.com/geeksandgod
login | register

taxonomy madness...

I've been stuck on this for some time and cannot find the answer in any of the usual places. This probably means that it is so easy that "everyone knows how to do this..."...

I am theming a CCK node, via a node-something.tpl.php file. I can access all of my CCK fields, except for the taxonomy tags.

Using the devel module, I can see the following:

taxonomy

Array
(
    [25] => stdClass Object
        (
            [tid] => 25
            [vid] => 5
            [name] => general
            [description] => placeholder catgeory
            [weight] => 0
        )

)

So, if I want to access the "name" field, for example to simply print out the word "general", how to I reference this?

It is the "stdClass Object" that has me confused - I am a PHP4 guy, and I have not worked with classes before. If it was a simple array (like all the other CCK fields) then I would be sweet, but clearly I need to be doing something different in order to access the name field in this taxonomy array...

can anyone please point me in the right direction for this?
Thank you in advance!
Cheers
Pete.

Some class information

Accessing information in a class is easy.

With an array you would do something like $array_name['array key'] to get to something in an array. With objects it works in a similar way. It would be $object->key.

So an object in an array would be $array_name['array_key']->key. In this case $taxonomy[25]->name would get you the name of the taxonomy object.

Objects in PHP5 are really powerful and worth learning.

Matt Farina
Geeks and God Co-Host
www.innovatingtomorrow.net
www.mattfarina.com

I still aint got no class...

Thanks, Matt -

that's what I thought, however when I try to print this from inside my node-content-type.tpl.php file, the contents appear to be empty/blank.

Here's the line from my .tpl.php file:

<?php
print $taxonomy[25]->name;
?>

Do I need to call some other function first, to retrieve the taxonomy info? I would have thought that once node-content-type.tpl.php runs, we would have all the node information (including the taxonomy array) loaded and available. Certainly that seems to be what the devel module is telling me from the "dev_render" tab. And all the CCK fields seem to be there.

I'm reasonably certain that I haven't got any typos or syntax errors; I'll keep looking into this.

Not quite there

In the case of your template the $taxonomy variable is just a string.

Try this instead:
$node->taxonomy[25]->name;

I think that's it but I can't quite remember.

Matt Farina
Geeks and God Co-Host
www.innovatingtomorrow.net
www.mattfarina.com

thank you thank you thank you!

And again... thank you.

that last method worked. Pretty confusing to a newbie like me however... All the additional CCK field types are addressed directly, but the body (which I was printing out already) and taxonomy is different.

I was already printing out the body,

<?php
print $node->content['body']["#value'];
?>
so the addressing method was staring me in the face the whole time.

Blessings & thanks!
Pete

Part of the SAP Network