How To Add a Custom Class To the_tags in WordPress

Home » Blog » WordPress Development » How To Add a Custom Class To the_tags in WordPress

Do you want to add a custom class to all the links output by the_tags function? It’s really easy.

The the_tags function is a template function that lets a theme display all the tags related to a post. It works on single posts and also inside the Loop that displays the list of blog posts.

Add this snippet to your functions.php class or create a custom plugin and add this code to it:

function add_custom_class_to_the_tags_html($html){
    $html = str_replace('<a', '<a class="my_custom_tag_class"', $html);
    return $html;
}
add_filter('the_tags', 'add_custom_class_to_the_tags_html', 10, 1);

The above code will add a new filter to the the_tags function. The add_custom_class_to_the_tags_html function will be called whenever the_tags function is called, and it will add a custom class to all the links.

Replace my_custom_tag_class with whatever class name you want to be added to all the links output by the the_tags function.

Leave a Reply

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

Register an account to save your snippets or go Pro to get more features.