say i have a category called cars and a article in that category called ford. How would I get the name of the category that the article ford falls under (in this case cars)? print_r($node); prints out cars but how do i print out just cars?

Comments

eojthebrave’s picture

Are you trying to do this in your theme or somewhere else in your code? Could you give an example of the code that you do have, and maybe a description of what you're hoping to accomplish. Maybe it's just me, but I'm not quite sure what you're looking for.

zilla’s picture

you could create a page that's a big php snippet that pulls up everything ford, but the title of the page itself is "cars" or whatever you want to call it...tons of these in the handbook (documentation - php snippets - pages or blocks)

ggarry’s picture

"Are you trying to do this in your theme" yes in page.tpl.php like I am doing for a custom field $node->field_custom[0]['value'] but I want to get the category the node is under.

ggarry’s picture

no one?

zilla’s picture

could you provide a literal example - like what the title of a sample node would be and what you would like it to say, and what that 'said statement' references? i'm a bit confused and think you may be able to do this with a simple edit or vocabulary - unless i'm totally misunderstanding you...

ggarry’s picture

Ok I have a vocabulary called vehicles and terms cars,trucks,bikes etc I have a article/page or what ever drupal calls them (node?) called ford now on this article i just want to be able to print off anywhere I want the category the article is under (cars in this case) so if I put print_r($node); anyhere on page.tpl.php a section looks like this.

Array ( [68] => stdClass Object ( [tid] => 68 [vid] => 1 [name] => Cars [description] => [weight] => 0 ) ) [files] => Array ( ) )

So I want to print Cars in my page.tpl.php.

zilla’s picture

i sort of follow you now - though i do this differently: if you mouse over the term 'car' under vocab vehicles, there will be a number associated with it in the full url - you can link to that menu item directly and it will display as the page term automatically...for example,if cars is term 4 under vehicles, then taxonomy/term/4 should produce a page with title 'cars' and all nodes tagged or assigned to that term...if you're trying to call the term within the node itself or in some other place, you may need a custom link (in fact, i believe that the custom link module may be able to accomodate this but i'm not sure, don't use it myself)...or perhaps custom breadcrumbs module (but that could put the text in the wrong spot)...or perhaps a CCK field that calls the terms/vocabulary (another route for this)

eojthebrave’s picture

You might be able to use taxonomy_node_get_terms_by_vocabulary (http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary/5)

Given a node ID, and a Vocabulary ID this will return all terms associated with the node. You could then loop over the terms and print them out as needed.

$node_terms = taxonomy_node_get_terms_by_vocabulary(<node.id>, <vocab.id>);
foreach ($node_terms as $term) {
 print $term->name;
}
ggarry’s picture

Excellent thanks eojthebrave.

$node_terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $id);