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

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;
}

Leave a Reply

Your email address will not be published. Required fields are marked *