D7 - Print node taxonomy terms including custom term field as CSS Class
Are you cool with using views?   I do this type of thing all the time on my site.  If you are then reply back and I'll post an example.