Hi All,

I want to know how to add the icon/ small picture to each taxonomy for identifying each category. Any modules can do that?

Thanks
Treo

Comments

devlond’s picture

I wonder what the best practice here is. I'm new to community development, so I am ignorant to the protocol.

There's a couple of choices I've found so far:
-modify the node.tpl.php file, add html/css there (I think this might be the least favored)
-modify the style/layout.css for your theme
-make your own style.css, call it from the node.tpl.php
-make a node-.tpl.php for the content types that you want modified

Again, drupal newb, so what's the most widely accepted way to do this?

Chill35’s picture

modify the style/layout.css for your theme

In Drupal 5.x, each taxonomy term when displayed under the node has its own CSS hook.
For example, the XHTML to print the term "love" could be, if the term id is 9 :

<a class="taxonomy_term_9" title="" rel="tag" href="/taxonomy/term/9">love</a>

Now that you have your hook, you can style it, in your {theme_name}/style.css file :

a.taxonomy_term_9 {
/* styling */
}

You set the icon as a background-image to the anchor element, and you push the text of the link (i.e. "love") to the right using the CSS property padding-left to make room for that image.

I wrote an article on how to insert a small icon next to links or headlines here :
http://11heavens.com/icon-to-the-left

Caroline
A coder's guide to file download in Drupal
Who am I | Where are we
11 heavens

devlond’s picture

Firstly, 11heavens.com is great. I found it the other day.

Secondly, thanks!
I suppose a person could put some php in that class title so it would use the 'tag' name of the tax. term instead of the id? But I cna't find the opposite of taxonomy_get_term($tid) to get an id when passed a term. I must be missing something?

Chill35’s picture

Thank you! 11heavens.com is... good and needs improvement.

I suppose a person could put some php in that class title so it would use the 'tag' name of the tax.

I suppose someone could do that, but I wouldn't look for a way to do this myself. I'll tell you why. When a node is loaded, we have "free" access to all its terms as "ids" - that's a given. For each id that you may want to retrieve the human-friendly name for, you will need to query the database. Now it's a known fact that database queries are the tallon d'achille of Drupal, so adding php to query the database, directly or by using a Drupal function such as http://api.drupal.org/api/5/function/taxonomy_get_term, just to get a name that may change (you may edit the name at some point from annoucement to news for example) is not worth it, if you only want to use that name for styling, in my opinion.

If you want to style a term with an icon, use the existing class and ids...

By the way, you can print for your own reference a list of all your terms with their ids. How do you retrieve this information ? When you click on a term, it brings you to a page where the id is in the path : http://mywebsite.com/taxonomy/term/8 for example. And if I am not mistaken, you can access the term names and associated ids on the category page in your admin panel, but I am not sure about that. Or you can use phpMyAdmin to examine the table {term_data}.

If you want to use the function taxonomy_get_term(), know that with each call to that function you will have one more database query (in table {term_data}). Here is how to use it :

$term_object = taxonomy_get_term($id);
$name = $term_object->name;
But I can't find the opposite of taxonomy_get_term($tid) to get an id when passed a term.

This can come handy in some instances. When for example, you want to build a lexicon...

The function is : taxonomy_get_term_by_name() and is documented here :
http://api.drupal.org/api/5/function/taxonomy_get_term_by_name

$found = false;
$term_objects = taxonomy_get_term_by_name('love');
foreach ($term_objects as $term_object) {
   if ($term_object->name == "love") {
     $term_id = $term_object->tid;
     $found = true;
  }
}

The reason why I am checking twice for the word love here is that taxonomy_get_term_by_name looks for all termS that have love in it : love of my life, I love Michigan, love me true, love, droolalloverme, you get my point.

Caroline
Who am I | Where are we
11 heavens

Chill35’s picture

Actually you can acess the term names through $taxonomy in node.tpl.php.

If you want to write the markup for the taxonomy terms list, you need to either create a function in your template.php file just to style that list, or do the whole thing by hand in node.tpl.php, replacing print $terms with a block of code that goes through the $taxonomy array... It can be done. Without any extra database query.

Caroline
A coder's guide to file download in Drupal
Who am I | Where are we
11 heavens