2

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!

1 Answer 1

3

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;
}
3
  • 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 Feb 14, 2013 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, 2013 at 19:03
  • aaaa I looked up the code and found the hook. now I'm beginning to understand how filter works. thanks again! Feb 14, 2013 at 23:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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