By jacobmn on
On my site I have a node type that can only be assigned one category per node, and I would like each node to simply print the term that applies to that node. I know that I can print terms using:
<?php if ($terms): ?>
<span class="terms"><?php print $terms ?></span>
<?php endif;?>
But rather than an unordered list with a single term that is a link to that category, I would like to have the node simply print out the raw term name into my html so that I can do whatever I want with it. This is probably a really simple piece of code but I can't find any reference to doing this anywhere. Thanks for any help.
Comments
if there's only one term,
if there's only one term, then you should be able to use taxonomy_node_get_terms
===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz
Well, there are numerous
Well, there are numerous terms within the category. But only one can be assigned to each node, so I want to simply print that one term out in the node. No html, just raw data, so I can plug it in wherever I want in the node.tpl.php file.
And?
If there is only one term attached to that node, the array taxonomy_node_get_terms() returns will only have one item containing the raw data for that one term.
It sounds pretty much exactly like what you want.
--
Anton
Ok, so after playing with
Ok, so after playing with this and searching and searching I have to come crawling back with my tail between my legs. I cannot figure out how to use the taxonomy_node_get_terms array in my node.tpl.php file. When I use:
I just get the word "Array" output into the node. I am sure this is very newbish and I need to study up on my php and drupal API knowledge, but a little bump in the right direction - even just a link to some vague explanation - would be very helpful. Thanks again!
you can use print_r to see
you can use print_r to see the structure of the array you need to manipulate.
===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz
So when I use print_r to
So when I use print_r to look at the structure of the taxonomy array, I get this:
So based on that I would assume this would work:
But all I get then is no output at all in the node, but no errors. What am I doing wrong?
Have a look at this
Check out this page.
http://drupal.org/node/53089
Great, thanks much for that
Great, thanks much for that link! Based on some of their modified code I have come up with this method for displaying simply the term name with no links, no frils. I have mine printed out in a span, but obviously this could be modified to fit within any element.
Subscribing, Do you know
Subscribing,
Do you know how to get this as an option for a cck-field,to get the term-description printed within a cck-node as a field?
Thanks a lot in advance for your answer!
Greetings,
Martijn
For CCK try: <?php print
For CCK try:
print content_format('field_cck_field_name', $field_cck_field_name[0]);Retain "field_" and replace "field_name" with the actual name of your CCK field.
Does anyone know how to use
Does anyone know how to use this in D6?
Thanks for any help.
My new EasySnoozing, Nursing and BusinessEgghead websites on D7!
_
The only obvious thing that's changed for d6 that i see is the taxonomy_node_get_terms_by_vocabulary function. Change to
taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid)Thanks very much, that's what
Thanks very much, that's what the problem was!
My new EasySnoozing, Nursing and BusinessEgghead websites on D7!
How do you print this with
How do you print this with first/last li class?
In D6
... I did it like this:
Explanation:
First, I don't like the solutions where taxonomy functions are called. Especially if you are going to use them in preprocess functions. Everything you need is already stored in $variables. Why call a function which fetches it again from the database?
Anyway, the idea is to use theme_item_list() instead of theme_links to make an unlinked list. Overriding the node preprocess function is the easiest way to achieve this. The thing is that both functions want the taxonomy data passed in different formats. That's why I need to take $vars['taxonomy'] - which has the taxonomy data - and put it through a foreach before I can pass it to the theming function.
Everything else (floating, in line, backgrounds,...) is - of course - just plain old CSS trickery.
_preprocess_node(&$vars) {
_preprocess_node(&$vars) {
$data = array();
foreach ($vars['taxonomy'] as $term) {
$data[] = $term['title'];
}
$vars['terms'] = theme('item_list', $data, NULL, 'ul', array('class' => 'items inline'));
}
This works great. If i have two vocabularies, i should separate them; currently it is showing all the terms regardless of vocabulary.
Use PHP string functions
I had a content type with one category assigned to each node. I wanted the title to be [category]: [title] so I simply did this:
And my suggestion
Hello. i want to print taxonamy term in a node and see this link (#224171) useful.
This code work for me:
I hope this help.
Bye :)
I would suggest Display Suite
I would suggest Display Suite module or some jQuery