According to this book page the $terms variable should still be available in Drupal 7 for when someone like me wants to do something like spit out a list of taxonomy terms associated with a particular node. However like the user that commented on that page, I get a php notice:
Notice: Undefined variable: terms in include()
followed by the line number of the node--type.tpl.php file the code is in.
So...seeing as the pattern seems to be
echo $content['field_something'];
I tried that with the term reference field (unimaginatively labelled field_categories) and got:
Array
The next logical step (to me anyway) was echo print_r($content['field_categories']); which spat out a seething nest of arrays. I tried working my way through the arrays to get it to echo what I wanted but I'm not convinced I'm doing it right.
So do I have to do one of those slightly long winded load $node and write a script for it in a template.php or do a micromodule or is there something wonderfully simple like a checkbox that I've managed to miss in a week or so of overthinking it? :)
Thanks!
Comments
render
Content in D7 is a 'renderable array' rather than a string, so you need to pass it to the render function.
if you are printing afte the main content be sure to
hide($content['field_something'])first.-=o=-
Of course! Well I feel sheepish *turns into a sheep* XD
I'm not sure how I managed to completely miss that for ages, considering I've been echo rendering $content['body']s in various node--type.tpl.phps :|
So got categories showing up, now to work out what is wrong with my free tagging vocabulary, it's not showing up with echo render $content['field_tags'], it's slightly demented in Views and it spits an appreciable PDOException when I try to edit a node that has tags (though creating a node with tags isn't a drama).
Thank you, very much obliged!
works at bekandloz | plays at technonaturalist
But this assumes that we know
But this assumes that we know the name of the field in advance, which is fine if writing a theme for yourself, but impossible if writing a general purpose theme for anyone to use.
Is there really no D7 version of $terms ?
-=[o]=-
Three months later, I'm wondering if there is a way to send anything that identifies as a term reference through a preprocess in template.php to spit out as a $terms variable. Seeing if it's actually do-able and doing it are a bit beyond my current capabilities though :)
Edit: I just checked Bartik's template.php, they have:
So do-able, just long winded :)
works at bekandloz | plays at technonaturalist
...
That is not what this code is doing - all its doing is rewriting the standard field markup to use list code (ul, li) instead of div's (for taxonomy reference fields), Bartik still prints the actual field via $content, take my word for it, I wrote it ;)
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
-=[o]=-
Yeesh, I keep forgetting about my old topics. Thank you for clarifying. Still learning :)
works at bekandloz | plays at technonaturalist
-=[o]=-
Yeesh, I keep forgetting about my old topics. Thank you for clarifying. Still learning :)
works at bekandloz | plays at technonaturalist
Print Taxonomy Fields without link
Thank you, very much your code is working for me.
I have change in given line it work perfectly for me.
Thank you again
No answer yet, but someone
No answer yet, but someone else trying to get terms for a node:
taxonomy_node_get_terms() in Drupal 7
...
The answer is in the very first comment.
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Well, the first comment
Well, the first comment suggests
but that will print the content of one field only, not all term reference fields. I see that Zen theme uses
so I will try that. Another way to add markup to your terms, is to use custom templates in your theme folder for field--taxonomy_term_reference.tpl.php and/or taxonomy-term.tpl.php files.
Maybe this can help
Maybe this can help. With this code you can get all terms name from taxonomy field: