Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I need to add a css class to every <a> element retrieved by the_terms() (or similar function) in order to open the taxonomy permalink in a fancybox modal window. I've been searching this whole afternoon for a Filter that could handle it, with no success.

Anyway, I got into this (poor) solution below:

$cities = get_the_terms($post->ID, 'cities');           

        foreach ($cities as $city) {
            echo '<a class="fancybox" href="'. get_term_link( $city->slug, 'cities' ).'">'.$city->name. '</a>' ;
        }

But it's too 'ugly' to put in a template file.

Is there any wordpress filter that deals with this question of permalink html generation?

Thanks a lot!

share|improve this question

1 Answer 1

up vote 2 down vote accepted

The filter you are looking for is term_links-$taxonomy, where $taxonomy is the taxonomy name. This will filter the $term_links links array before outputting by the_terms():

add_filter('term_links-cities', 'ad_filter_links');

function ad_filter_links($term_links) {
    foreach ($term_links as $term_link) {
        $term_link = str_replace('<a ', '<a class="fancybox"', $term_link);
    }
    return $term_links;
}
share|improve this answer
    
whoah! that's what i'm looking for. Thanks you very much! By the way, how did you search this hook? I spent hours on google with no success in finding a similar suggestion –  Susan Siqueira Feb 14 '13 at 18:54
    
@SusanSiqueira you are welcome. I rely on the source code to find hooks, this one from get_the_term_list() function used by the_terms() which both are located in wp-includes/category-template.php. –  Ahmad M Feb 14 '13 at 19:03
    
aaaa I looked up the code and found the hook. now I'm beginning to understand how filter works. thanks again! –  Susan Siqueira Feb 14 '13 at 23:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.