[Note: An alternative method using template.php
can be found here: http://drupal.org/node/133223 ]
I figured out a way to sort term links by vocab - this actually works (!) in node.tpl.php 465 (that's only for phphtemplate themes). It does look a bit clumsy - If anyone can tidy it up a bit I'd be very grateful. It probably belongs in the template.php really ... but I'm happy to just have it working at all!
What it does is create a new set of links for each vocab group of terms rather than just having all terms in a single row. eg the code below would produce something like:
# Submitted by: JohnG on 08 Mar 2006
# Forums: newbies - hacking - drupal
# Articles: contributed - in good faith - GPL
Please note, that the code below only processes terms for a single vocab each time - in the example (vid) 27 and 28. You need to replace all the occurrances of 27 with the vid of your favourite vocab.
You can copy&paste these code-chunks as often as you like to cover all your vocabs if you like, as long as you remember to edit all the '27's correctly it will work.
One more thing, I put all instances of this vocab filter script between the <?php if ($terms): ?> ... <?php endif; ?>
tags and it works happily.
so it goes something like:
<?php if ($terms): ?>
<?php /* sort taxonomy links by vocabulary 27 */
$terms27 = taxonomy_node_get_terms_by_vocabulary($node->nid, 27);
if ($terms27) {
print '<div class="terms">Forums: ';
foreach ($terms27 as $key => $term27) {
$lterm27 = l($term27->name, 'taxonomy/term/'.$term27->tid);
print $lterm27.' - ';
}
print '</div>';
}
?>
<?php /* sort taxonomy links by vocabulary 28 */
$terms28 = taxonomy_node_get_terms_by_vocabulary($node->nid, 28);
if ($terms28) {
print '<div class="terms">Articles: ';
foreach ($terms28 as $key => $term28) {
$lterm28 = l($term28->name, 'taxonomy/term/'.$term28->tid);
print $lterm28.' - ';
}
print '</div>';
}
?>
repeat for each vocab vid as needed and then close the if ($terms):
thingamy:
<?php endif; ?>
enjoy!
Comments
Generic version
If you want a more generic version of this the following code will loop through all vocabularies and output the related terms:
Cheers
Dave
tooltips
Thanks a lot for this code, I was looking exactly for this :-)
I made one single change. Normally categories will add the term description as a tooltip (title attribute), and the code above is not doing this. Just add one more parameter to the function that creates the link:
Minor Thing
This is also exactly what I was looking for! Thank you!
Would it be possible to separate the multiple terms under the same vocab with a comma or straight bracket? Can you tweak the snippet? I would greatly appreciate it.
Sorry for the delay...!
If you haven't sorted this yet here is the code to add a comma - for a straight bracket change the comma on line that reads:
if ($terms):
endif;
Trim added
I just added a trim to remove the last separation:
And you can use the code at /node/38768 to get a solution for alpha sorting.
Now I use t() for terms
Now I use t() for terms too:
Using taxonomy_term_path
I modified the code to use taxonomy_term_path. This is for vocabularies 1 and 2:
Sort terms when t() is used
To sort alphabetically terms for i18n sites, I just tried to apply asort to the $tags array. I then needn't anymore of my previous use of db_query(). It works but not when l() is used. The following code then doesn't display hyperlinks of terms, it just displays the terms as text.
The following sample if for the vocabulary 5.
I don't know how to sort $tag if
$tags[] = l(t($term->name), taxonomy_term_path($term));
Ronan
print the term description
Hi there,
If you're using "t" for terms how then do you also print out the term description as above?
I'm trying to print out the term used within a vocabulary on a cck node , but also include the term description.
Thanks for any help (i'm a tragic php dunce)
-d
semantic markup
Thanks all, this is a very useful snippet. I made a small change, for those who care about the semantic web: I put the results in a definition list. My code:
adding class to taxonomies for styling purposes
Hi All, i've been based on this code to add the vocabulary name as a class to an unordered list of taxonomy items. This way you can later customize with CSS the style each taxonomy item will have depending on the vocabulary they belong.
- I've deleted the separator
- the unordered list tags (ul and li) add semantics to the code.
- I think it's easy to do (i'm not a php or drupal expert) and it should be in core, adding extra class to all elements will help a lot Drupal to be more popular for web designers!.
-I've put this in the taxonomy section of node.tpl.php from the Zen theme, not sure if this is the best way to do it.
Here you have an screenshot of what you can do with this and a little of CSS
And here is the CSS beyond it (not yet ready for IE):
icons belong to the Silk pack created by famfamfam
...
Just to let you know: these add extra SQL queries to your page (they are not cached).
And instead of:
Better use:
(So that 'forum.module' and 'image_gallery.module' and lots other can specialize the link.)
D6 for all Vocabularies-and add Vocab to class div
HI all,
I need to add a specific div class to each vocabulary, so I came up with this variation (see the print statements):
So now my
ie
I have three vocabularies: oranges, pears and apples.
Splitting up the print functions lets me print out this:
With my small php skills, this took me a while to figure out, so I'm posting to save someone else some time.
Connie
works for 6
Thanks ConnieC, this worked great to display vocabularies on separate lines. One question, how can you separate the terms with a comma or other symbol using this code?
actually comment http://drupal.org/node/53089#comment-1493216 worked this all out.
connie, if it was possible to
connie, if it was possible to jump through a computer, my arms would be latched around you! this is perfect for something i've been scratching my head over. THANK YOU!
Drupal6?
Is there anyway do update this to work with drupal6 too?
Thx
Drupal 6
Works for me in D6...
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Problem
I got a problem with this snippet:
I got 2 taxonomy term, with this snippet when I print the first one (number 1) everything is ok; but in the second call (taxomony term nr2) I have all the terms (taxonomy 1+2) and not the taxonomy 2 alone.
Any suggestion?
Arr, I see, yes me so lazy,
Arr, I see, yes me so lazy, sorry... wipped that out a bit too fast by the l@@ks..
See the "$items" bit, add the vocab id to it and it should solve your problem... i.e...
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Subscribing, any solution
Subscribing,
any solution for not doing a database-query on D5?
greetings, Martijn
Sorting Taxonomy on Node.tpl.php in Drupal 5
I have tried many examples to sort my terms alphabetically from one vocabulary and print them on my node.
Below is the solution that worked for me.
http://drupal.org/node/133223#comment-634019
I believe the difference is that this version is calling the node's taxonomy which is already sorted alphabetically rather than calling all the terms that are associated with the node which seems to default to sorting by the term id (tid) or weight rather than the term name.
taxonomy_node_get_terms_by_vocabulary — gets the terms but orders them by weight then tid.
where as
$node->taxonomy gets the terms from the node without ordering by weight or tid
Please let me know if my assumptions are wrong.
Hopefully this will help others.
I'd like to print out terms
I'd like to print out terms from one vocabulary in an orderly manner which this snippet more than does, however if I add a php print $terms anywhere after (and only after, if I use the standard php print $terms beforehand everything works alright) this snippet the output is "array". Do you or does anyone know why this is? I would like to have terms from other vocabularies printed underneath the vocabulary that comes out orderly.
better approach...
All round this is a better method:
http://11heavens.com/putting-some-order-in-your-terms
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
A very odd issue
I've put this snippet into my node.tpl.php, and oddly enough it works for one term and its subterms, but for other terms and its subterms they are displayed backwards (subterm 3 << subterm 2 << subterm 1 << term). The hierarchy is still accurate (ie if I were to click on subterm 1 it would be recognized as subterm 1 and not subterm 3) so I know it is not an error on the part of the taxonomy module.
I'm using a very cut down
I'm using a very cut down version of this in an attempt to perform some theming on my site. In one instance I've got:
happily printing the name of a vocab term into a little bit of inline styling in node.tpl.php and it works perfectly.
I thought I could use the same code to perform a similar trick in a user created block to insert an image based on the page's vocabulary terms... but it doesn't work there.
in that case I have
but the image doesn't show, only the alt text. Checking the page source in firefox shows that it's printing
...which obviously doesn't exist. It seems the PHP is simply being ignored, despite the block source being set as php code in the input format area.
more basic php such as
is printed fine.
I presume the problem is that the variables I'm trying to call are just meaningless in this context, but I'm pretty new to drupal and PHP so I'm not sure where those variables are being defined or under what circumstances I'm allowed to call them. Any pointers would be most gratefully recieved.
theJT, This page (a
theJT,
This page (a documentation page in the theming guide) is a place to share working code, not a place to request individual support. Please:
- post your question on the forum
- ask a site maintainer (http://drupal.org/node/add/project-issue/webmasters) to remove both your comment and this one
template function for Drupal 6
Step one
Add the following snippet to your template.php file:
Step two
Now add the following to your node.tpl.php file:
if the vocabulary has vid 1
if you want custom label
Have fun!!
don't work
hi for me don't work in you node.tpl.php you say if you add
print yourthemename_print_terms($node, '$vid');
but i don't have a function yourthemename_print_terms ?? how do you make?theme overrides
Read the documentation about theme overides, it is important to understand how they work. Once you understand theme overrides, you will have learned that in the code above, "yourthemename" should by replaced by the name of your theme.
wrong function name in the node.tpl.php usage.
You say to use "yourthemename_print_terms($node, 1)"
When it should be the same as your function name in template.php
"yourthemename_get_terms_by_vocabulary(...)"
] duran goodyear
] alphex information solutions, llc.
] http://alphex.com
I second to this. Function
I second to this. Function name should be the same.
drupal 6 version
if ($terms):
endif;
I changed $node->vid parameter by $node parameter because must be a object for D6 (see http://tinyurl.com/3ypdyfa).
someone delete my error?
someone delete my error?